简体   繁体   中英

How should this c++ typedef using decltype and declval be written to make it portable?

I have the following

template <typename F, typename A0>
struct ResultOf {
        typedef typename decltype(boost::declval<F>()(boost::declval<A0>())) Type;
};

It was written so that VS2010 could have a result_of that worked for a specific use case. It is working under vs2015, vs2013 and vs2010 but under gcc I get a compile error

error: expected nested-name-specifier before ‘decltype’
typedef typename decltype(boost::declval<F>()(boost::declval<A0>())) Type;

Is there an obvious small fix here?

typename keyword is not needed here. It is used, in particular, to denote a dependent type, like T::value_type , when a compiler cannot know whether value_type is a type. There are no dependent types in the present case.

Removing the typename works in clang, gcc, and modern msvc:

https://godbolt.org/z/CfOw-_

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM