c++ - Using `std::conditional_t` to define a class' `typedef` in dependence of its template parameter -


i try use std::conditional_t define class a's typedef in dependence of template parameter t:

template< typename t > class {   public:     typedef std::conditional_t< std::is_fundamental<t>::value, t, decltype(std::declval<t>().foo())> type; };  template< typename t > class b {   public:     t foo()     {       // ...     } };   int main() {   typename a< int >::type = 5;    // causes error   typename a< b<int> >::type b = 5; // not cause error    return 0; } 

unfortunately, code not compile. error:

error: member reference base type 'int' not structure or union         typedef std::conditional_t< std::is_fundamental<t>::value, t, decltype(std::declval<t>().foo())> type; 

does know how fix it?

both types in conditional expression must valid, isn't sfinae context.

you can achieve want "executing" chosen class template:

typedef typename std::conditional_t< std::is_fundamental<t>::value, identity<t>, foo_t<t>>::type type; 

with defined as:

template<typename t> struct identity{ using type = t; };  template<typename t> struct foo_t{ using type = decltype(std::declval<t>().foo()); }; 

demo


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -