This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 > |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template < class Iterator > | |
std::vector( Iterator, Iterator ) -> std::vector< typename Iterator::value_type >; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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)...); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template < class T > | |
struct X | |
{ | |
X( T t ) { } | |
}; | |
int main() | |
{ | |
X x1(0); // X<int>. | |
X x2(0.0); // X<double>. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
}(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <limits> | |
#include <utility> | |
template < class T > | |
constexpr | |
decltype(std::declval<T>()*2) | |
twice(T a) { | |
return a*2; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// エラー、SFINAEの文脈でconstexprラムダが使われている | |
template < typename T, | |
bool b = []{ | |
T t;t.func(); | |
return true; | |
}()> | |
void f() { | |
T t; | |
t.func(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main(){ | |
auto twice = [](int n){ return n*2; }; | |
// twiceのoperator()は自動的にconstexpr指定されているのでOK | |
constexpr int x = twice(2); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main(){ | |
// ラムダ式にconstexprをつけることができる | |
auto f1 = []() constexpr {}; | |
// OK | |
auto f2 = []() mutable constexpr {}; | |
// これもOK | |
auto f3 = []() constexpr mutable {}; | |
} |