Created
June 18, 2019 17:58
-
-
Save Karsten1987/a92fe0ba37fe0b873f578b35beb42340 to your computer and use it in GitHub Desktop.
template error message
This file contains hidden or 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> | |
| using namespace std; | |
| struct Callable | |
| { | |
| void operator()(std::string s) { cout << "call my struct with " << s << endl; } | |
| }; | |
| struct Assignable | |
| { | |
| Assignable & operator=(const Assignable & other) | |
| { | |
| return *this; | |
| } | |
| }; | |
| template<typename T> | |
| auto fcn(T&& caller) -> decltype(declval<T>().operator()(std::declval<std::string>())) | |
| { | |
| caller("ohh baby"); | |
| } | |
| template<typename T> | |
| auto fcn(T&& assignable) -> decltype(declval<T>().operator=(std::declval<Assignable>())) | |
| { | |
| Assignable other; | |
| other = assignable; | |
| cout << "assigning stuff" << endl; | |
| } | |
| template<typename T = void> | |
| auto fcn(...) | |
| { | |
| static_assert(!std::is_same<T,T>::value, "Can't call fcn like that. Has to be either callable or assignable"); | |
| } | |
| int main() { | |
| // your code goes here | |
| fcn(Callable()); | |
| fcn(Assignable()); | |
| fcn(string()); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment