Skip to content

Instantly share code, notes, and snippets.

@utilForever
Created February 14, 2017 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save utilForever/dfaf6f4c8154f635841b5967c15c17f3 to your computer and use it in GitHub Desktop.
Save utilForever/dfaf6f4c8154f635841b5967c15c17f3 to your computer and use it in GitHub Desktop.
std::declval
#include <utility>
#include <iostream>
struct Default { int foo() const { return 1; } };
struct NonDefault
{
NonDefault(const NonDefault&) { }
int foo() const { return 1; }
};
int main()
{
decltype(Default().foo()) n1 = 1; // type of n1 is int
// decltype(NonDefault().foo()) n2 = n1; // error: no default constructor
decltype(std::declval<NonDefault>().foo()) n2 = n1; // type of n2 is int
std::cout << "n1 = " << n1 << '\n'
<< "n2 = " << n2 << '\n';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment