decltype

How can I determine the return type of a C++11 member function

て烟熏妆下的殇ゞ 提交于 2020-02-21 10:08:41
问题 I am trying to determine the return type of a various C++ member functions. I understand that decltype and std::declval can be used to do this, but I am having problems with the syntax and finding useful examples. The TestCBClass below shows an example of a dumb class that contains a mixture static and normal member functions - with & without arguments and return types. Depending on the method in question, I would like to be able to declare a vector of return types from each of the various

Extract just the argument type list from decltype(someFunction)

谁说我不能喝 提交于 2020-02-01 20:46:51
问题 I have a variadic template that represents a list of parameters for a function, eg: void myFunc (int,int,std::string) { } template<typename... Args> class MyTemplateClass { }; ... MyTemplateClass<int,int,std::string> myConcrete; // for use with myFunc later Is there any way I can extract just the argument types from decltype(func) to save having to write them manually, eg: MyTemplateClass<something_like_decltype(myFunc)> myConcrete; ie decltype in this case would give me "void(int,int,string)

Member function call in decltype

江枫思渺然 提交于 2020-01-31 03:09:32
问题 The following code: struct A { int f(int); auto g(int x) -> decltype(f(x)); }; Fails to compile with the error: error: cannot call member function 'int B::f(int)' without object If I change it to: struct A { int f(int); auto g(int x) -> decltype(this->f(x)); }; I get another error: error: invalid use of 'this' at top level What is wrong with either of these? I am using gcc 4.6 回答1: Currently you can only access 'this' and members of the class inside the function body, but this is likely to be

auto, decltype(auto) and trailing return type

风格不统一 提交于 2020-01-23 11:06:32
问题 Is there a difference between: template <class T> constexpr decltype(auto) f(T&& x) -> decltype(std::get<0>(std::forward<T>(x))) { return std::get<0>(std::forward<T>(x)); } and: template <class T> constexpr auto f(T&& x) -> decltype(std::get<0>(std::forward<T>(x))) { return std::get<0>(std::forward<T>(x)); } and if so, what is it, and which one should I use for perfect forwarding? 回答1: Trailing return type should only be used with auto The point of decltype(auto) vs auto is to distinguish the

Does a placeholder in a trailing-return-type override an initial placeholder?

∥☆過路亽.° 提交于 2020-01-14 08:27:09
问题 g++ appears to accept any combination of auto and decltype(auto) as initial and trailing return types: int a; auto f() { return (a); } // int auto g() -> auto { return (a); } // int auto h() -> decltype(auto) { return (a); } // int& decltype(auto) i() { return (a); } // int& decltype(auto) j() -> auto { return (a); } // int decltype(auto) k() -> decltype(auto) { return (a); } // int& However, clang rejects j and k , saying: error: function with trailing return type must specify return type

Class member visibility in member function declaration signature

核能气质少年 提交于 2020-01-13 18:30:28
问题 Why does this work: template <typename A> struct S { A a; template <typename B> auto f(B b) -> decltype(a.f(b)) { } }; But this does not ( a and f swapped places): template <typename A> struct S { template <typename B> auto f(B b) -> decltype(a.f(b)) { } A a; }; saying that a is not declared in that scope (inside decltype) but adding explicit this-> makes it work. 回答1: template <typename A> struct S { A a; template <typename B> auto f(B b) -> decltype(a.f(b)) { } }; This works because within

What is decltype(0 + 0)?

我与影子孤独终老i 提交于 2020-01-11 08:05:08
问题 (Prompted by an answer.) Given N3290, §7.1.6.2p4, where the list items are unnumbered, but numbered here for our convenience: 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 program is ill-formed; otherwise, if e is an xvalue, decltype(e) is T&&, where T is the type of e

What is decltype(0 + 0)?

和自甴很熟 提交于 2020-01-11 08:05:07
问题 (Prompted by an answer.) Given N3290, §7.1.6.2p4, where the list items are unnumbered, but numbered here for our convenience: 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 program is ill-formed; otherwise, if e is an xvalue, decltype(e) is T&&, where T is the type of e

Conditionally choose a type with decltype() and the ternary operator

僤鯓⒐⒋嵵緔 提交于 2020-01-11 07:11:56
问题 I have a file a.cpp : #include <bits/stdc++.h> using namespace std; int main(){ int a=5; double b=4.3; decltype(a>b?a:b) n; cout << typeid(n).name(); } The Output of above Code is d but I expect it to be i as "a" is greater than "b" I am trying to learn about decltype. Can you please tell what I am missing here? I am using gcc version 6.3.0 (MinGW.org GCC-6.3.0-1). 回答1: C++ is a statically-typed language. That means, the type of a thing cannot depend on runtime criteria. For this reason, the

Conditionally choose a type with decltype() and the ternary operator

别来无恙 提交于 2020-01-11 07:10:48
问题 I have a file a.cpp : #include <bits/stdc++.h> using namespace std; int main(){ int a=5; double b=4.3; decltype(a>b?a:b) n; cout << typeid(n).name(); } The Output of above Code is d but I expect it to be i as "a" is greater than "b" I am trying to learn about decltype. Can you please tell what I am missing here? I am using gcc version 6.3.0 (MinGW.org GCC-6.3.0-1). 回答1: C++ is a statically-typed language. That means, the type of a thing cannot depend on runtime criteria. For this reason, the