Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Karsten1987/a92fe0ba37fe0b873f578b35beb42340 to your computer and use it in GitHub Desktop.

Select an option

Save Karsten1987/a92fe0ba37fe0b873f578b35beb42340 to your computer and use it in GitHub Desktop.
template error message
#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