nonsensation
nonsensation

Reputation: 3707

Conditional template variable

#include <type_traits>

template< typename T >
using cond =
    std::conditional_t<
        std::is_void< T >::value ,
        std::true_type ,
        std::false_type
    >::value;

static_assert( cond< void > , "" );

int main() {}

Example clang 3.6

missing 'typename' prior to dependent type name 'std::conditional_t::value, std::true_type, std::false_type>::value'

Why is it missing a typename as it isn't a type at all?
If I add the typename it throws this at me:

error: typename specifier refers to non-type member 'value' in 'std::integral_constant<bool, true>'

Isn't it the same thing as in here? How do I resolve this?

Upvotes: 3

Views: 853

Answers (1)

Pradhan
Pradhan

Reputation: 16737

You can only use template aliases to alias types. The right syntax for your variable template is

template< typename T >
constexpr bool cond = std::conditional_t< std::is_void< T >::value,
                      std::true_type,
                      std::false_type >::value;

While the error message your code gets is a bit confusing, adding the suggested typename results in this more helpful message with clang 3.6

error: typename specifier refers to non-type member 'value' in
       'std::integral_constant'

using cond = typename std::conditional_t< std::is_void< T >::value,
^~~~~                 std::true_type,
                      std::false_type >::value;

which explains the real problem.

Upvotes: 3

Related Questions