How do I get the return type of a member function without an object?
You don’t need that- remember that since decltype doesn’t evaluate its argument, you can just call on nullptr. decltype(((T*)nullptr)->foo()) footype;
You don’t need that- remember that since decltype doesn’t evaluate its argument, you can just call on nullptr. decltype(((T*)nullptr)->foo()) footype;
Using a hyperlinked C++ grammar, the parsing of decltype(void()) is: decltype( expression ) decltype( assignment-expression ) decltype( conditional-expression ) … lots of steps involving order of operations go here … decltype( postfix-expression ) decltype( simple-type-specifier ( expression-listopt ) ) decltype( void() ) So void() is a kind of expression here, in particular a postfix-expression. Specifically, … Read more
this is a prvalue, so decltype(this) should always be plain X* (or X cv* / cv X*). The addition of const& seems to be a bug in GCC (tested with g++ 4.8.1), which happens only for a class template (not for a “plain” class) and only inside the trailing return type (not inside the body … Read more
decltype (MyFunction_1) will give you the type of MyFunction_1 (i.e. the function type std::tuple<int, bool, double> ()), you need to emulate a function calling 1 (via adding ()) to get the return type (i.e. std::tuple<int, bool, double>), e.g. void MyFunction_2 (decltype (MyFunction_1()) ¶ms); // ^^ 1 The expression is evaluated at compile-time, the function won’t … Read more
It’s not an oversight. It’s interesting, that in Decltype and auto (revision 4) (N1705=04-0145) there is a statement: The decltype rules now explicitly state that decltype((e)) == decltype(e)(as suggested by EWG). But in Decltype (revision 6): proposed wording (N2115=06-018) one of the changes is Parenthesized-expression inside decltype is not considered to be an id-expression. There … Read more
Just above that example, it says if e is an unparenthesized id-expression or a class member access (5.2.5), decltype(e) is the type of the entity named by e. if e is an lvalue, decltype(e) is T&, where T is the type of e; I think decltype(a->x) is an example of the “class member access” and … Read more
decltype gives the declared type of the expression that is passed to it. auto does the same thing as template type deduction. So, for example, if you have a function that returns a reference, auto will still be a value (you need auto& to get a reference), but decltype will be exactly the type of … Read more
7.1.6.2 [dcl.type.simple] For an expression e, the type denoted by decltype(e) is defined as follows: — if e is an unparenthesized id-expression or an unparenthesized class member access (5.2.5), decltype(e) is the type of the entity named by e. If there is no such entity, or if e names a set of overloaded functions, the … Read more
decltype also considers whether the expression is rvalue or lvalue . Wikipedia says, The type denoted by decltype can be different from the type deduced by auto. #include <vector> int main() { const std::vector<int> v(1); auto a = v[0]; // a has type int decltype(v[0]) b = 1; // b has type const int&, the … Read more
You cannot use a lambda expression except by actually creating that object- that makes it impossible to pass to type deduction like decltype. Ironically, of course, the lambda return rules make it so that you CAN return lambdas from lambdas, as there are some situations in which the return type doesn’t have to be specified. … Read more