Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional template variable

Tags:

c++

c++14

#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?

like image 227
nonsensation Avatar asked Oct 29 '25 16:10

nonsensation


1 Answers

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.

like image 164
Pradhan Avatar answered Oct 31 '25 05:10

Pradhan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!