decltype

Get the type of the return value in C++

大城市里の小女人 提交于 2020-01-11 01:01:53
问题 Suppose we have a function f which returns a value of some unknown type (let's call it T ) and takes a value of the type T as an argument (and possibly has some other arguments). How do I get the return type of f in C++14? There is a way to do it if we know the know the argument types (via std::result_of ). Is it possible if we know all the argument types except T ? Example: template <class F> // F is functor with T operator()(T a, T b) class A { // Here I want to do // T some_function(T some

Get the type of the return value in C++

≡放荡痞女 提交于 2020-01-11 01:01:07
问题 Suppose we have a function f which returns a value of some unknown type (let's call it T ) and takes a value of the type T as an argument (and possibly has some other arguments). How do I get the return type of f in C++14? There is a way to do it if we know the know the argument types (via std::result_of ). Is it possible if we know all the argument types except T ? Example: template <class F> // F is functor with T operator()(T a, T b) class A { // Here I want to do // T some_function(T some

Compiler can't deduce the return type?

会有一股神秘感。 提交于 2020-01-11 00:57:50
问题 I am trying to use the decltype keyword on an auto function: struct Thing { static auto foo() { return 12; } using type_t = decltype(foo()); }; And I get the following error (gcc 7.4): <source>:6:25: error: use of 'static auto Thing::foo()' before deduction of 'auto' decltype(foo()); ^ <source>:6:25: error: use of 'static auto Thing::foo()' before deduction of 'auto' Why has the compiler not yet deduced the return type? 回答1: Because for class definition, compiler will first determine all

decltype acting inconsistent when used in conjunction with conditional operator

☆樱花仙子☆ 提交于 2020-01-07 06:26:09
问题 While studying some of the new C++11 features, I observed some strangeness related to the new decltype keyword and its interaction with the conditional operator. I was very surprised to see the output of the following program: #include <iostream> #include <map> int main(void) { // set up a map that associates the internal compiler-defined type_info name with a human readable name std::map <std::string, std::string> types; types[typeid(decltype(static_cast<unsigned char >(0))).name()] =

C++11/14 and return( … ) vs return

给你一囗甜甜゛ 提交于 2020-01-04 03:47:29
问题 In C++ you are allowed to write a return statement that looks like : return ( ... ); which is different from the more popular : return ... ; In particular the first version returns the address/reference of something that is local to the stack of the function which contains that return statement. Now why something would like to return a reference to something that, at that point, has no lifetime ? What are the use case for this idiom ? Considering the new buzzword and features from C++11 and C

C++11/14 and return( … ) vs return

社会主义新天地 提交于 2020-01-04 03:47:02
问题 In C++ you are allowed to write a return statement that looks like : return ( ... ); which is different from the more popular : return ... ; In particular the first version returns the address/reference of something that is local to the stack of the function which contains that return statement. Now why something would like to return a reference to something that, at that point, has no lifetime ? What are the use case for this idiom ? Considering the new buzzword and features from C++11 and C

I'm not understanding these uses of std::result_of and decltype

让人想犯罪 __ 提交于 2020-01-03 05:51:07
问题 On my Visual Studio project I had the following and it worked fine: template <typename T> void function(T type) { std::result_of<T()>::type myVar = 5; // This compiled fine with Visual Studio, but with GCC it shows the following errors: //error: dependent-name ‘std::result_of<T()>::type’ is parsed as a non-type, //but instantiation yields a type. note: say ‘typename std::result_of<T()>::type’ if a type is meant } int main() { auto lambda = []() { return float(); }; function(lambda); return 0;

Order of declaration matters when using decltype for member function return type

北慕城南 提交于 2020-01-02 10:22:47
问题 Why does this work: template<typename Base, typename Acc> struct Foo { Base base; Acc acc; auto operator()(unsigned i) const -> decltype(acc(base(i))) { return acc(base(i)); } }; and this gives a compiler error: template<typename Base, typename Acc> struct Foo { auto operator()(unsigned i) const -> decltype(acc(base(i))) { return acc(base(i)); } Base base; Acc acc; }; error: there are no arguments to ‘base’ that depend on a template parameter, so a declaration of ‘base’ must be available [

Getting the type of a typename or expression

99封情书 提交于 2019-12-30 04:36:14
问题 Consider the following example. Somewhere in my code is a name x . I have no idea if x is a type or an object (it could be both). Is there any way to get the type of x , i.e., x itself if x is a type or decltype(x) if x is an object? I tried doing something as trivial as decltype(int) but this yields an error, since int is not an expression. Is there any substitute way to do this? I would like something like: typedef int l; mydecltype(l) x; // int x; mydecltype(x) y; // int y; How can I get

Why is Visual Studio 2013 having trouble with this class member decltype?

浪尽此生 提交于 2019-12-29 08:27:59
问题 #include <vector> struct C { std::vector<int> v; decltype(v.begin()) begin() { return v.begin(); } decltype(v.end()) end() { return v.end(); } }; Clang++ has no problem, but MSVC 2013 gives the following error: error C2228: left of '.begin' must have class/struct/union 回答1: This is a known bug in VS2013, fixed † in VS2015. The compiler will accept the code if you use a trailing return type instead. struct C { std::vector<int> v; auto begin() -> decltype(v.begin()) { return v.begin(); } auto