fork download
  1. #include <functional>
  2.  
  3. template<typename T>
  4. class Value {
  5. std::function<T()> get;
  6. public:
  7.  
  8. template<class Y, typename = decltype(std::declval<Y&>()())>
  9. Value(Y lambda ) : get( std::move( lambda ) ) {}
  10. };
  11.  
  12. int main()
  13. {
  14. Value<double> d1 = [] { return 1.; };
  15. Value<double> d2 = 123;
  16.  
  17. }
Compilation error #stdin compilation error #stdout 0s 4524KB
stdin
Standard input is empty
compilation info
prog.cpp:15:19: error: no viable conversion from 'int' to 'Value<double>'
    Value<double> d2 = 123;
                  ^    ~~~
prog.cpp:4:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'int' to 'const Value<double> &' for 1st argument
class Value {
      ^
prog.cpp:4:7: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'int' to 'Value<double> &&' for 1st argument
class Value {
      ^
prog.cpp:9:5: note: candidate template ignored: substitution failure [with Y = int]: called object type 'int' is not a function or function pointer
    Value(Y lambda ) : get( std::move( lambda ) )  {}
    ^
1 error generated.
stdout
Standard output is empty