Skip to content

Instantly share code, notes, and snippets.

View loliGothicK's full-sized avatar
:octocat:
may the --force be with you!

Mitama loliGothicK

:octocat:
may the --force be with you!
View GitHub Profile
template <class F>
struct Fixed {
F f;
template <class... Args>
decltype(auto) operator()(Args&&... args) const {
return f(std::ref(*this), std::forward<Args>(args)...);
}
};
template < class F >
template < class Iterator >
std::vector( Iterator, Iterator ) -> std::vector< typename Iterator::value_type >;
#include <iostream>
#include <functional>
template <class F>
struct Fixed {
F f;
Fixed(F f): f{f} {}
template <class... Args>
decltype(auto) operator()(Args&&... args) const {
return f(std::ref(*this), std::forward<Args>(args)...);
template < class T >
struct X
{
X( T t ) { }
};
int main()
{
X x1(0); // X<int>.
X x2(0.0); // X<double>.
#include <array>
#include <utility>
#include <iostream>
template < std::size_t N >
constexpr auto ints = []{
std::array<std::size_t, N> arr{};
for( std::size_t i{}; i < N; ++i ) arr[i] = i;
return arr;
}();
#include <limits>
#include <utility>
template < class T >
constexpr
decltype(std::declval<T>()*2)
twice(T a) {
return a*2;
}
// エラー、SFINAEの文脈でconstexprラムダが使われている
template < typename T,
bool b = []{
T t;t.func();
return true;
}()>
void f() {
T t;
t.func();
}
int main(){
auto twice = [](int n){ return n*2; };
constexpr int (*func_pointer_to_twice)(int) = twice;
static_assert(func_pointer_to_twice(2) == 4);
}
int main(){
auto twice = [](int n){ return n*2; };
// twiceのoperator()は自動的にconstexpr指定されているのでOK
constexpr int x = twice(2);
}
int main(){
// ラムダ式にconstexprをつけることができる
auto f1 = []() constexpr {};
// OK
auto f2 = []() mutable constexpr {};
// これもOK
auto f3 = []() constexpr mutable {};
}