简体   繁体   中英

std::void_t and nested non type members

I have the following code where I get unexpected result (second static_assert fails):

#include <type_traits>

template <typename T, typename = void>
struct is_bananas : std::false_type {};
template <typename T>
struct is_bananas<T, std::void_t<typename T::config::num_items>>
    : std::true_type {};

struct Config{
    static constexpr int num_items=42;
};

struct Bananas{
    using config = Config;
};

static_assert(is_bananas<int>::value == false);
static_assert(is_bananas<Bananas>::value == true);

When I use T::config instead of T::config::num_items code works as expected.

If I use decltype around num_items then it works.

Am I correct to assume that void_t only works with types?

Is there a nicer way to do what I want?
If somebody is confused by this(and think: just throw a decltype ) - I find decltype hard to read in real code when there are long names and deep nesting so if there is a nicer way to do this with void_t or any other template code I would like to know.

Is there a nicer way to do what I want?

Nice and nicer are subjective.

By example, I find very nice your decltype() solution.

Anyway... a possible alternative is define a substitute for std::void_t that works with value types

Something as

template <auto...>
using value_void_t = void;

and write

template <typename T>
struct is_bananas<T, value_void_t<T::config::num_items>>
   : std::true_type {};

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