Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'using type' in C++ causes several errors

In my question type as returntype in c++ I was provided with an answer that gave me such a structure:

template <int N>
struct int_type {
    using type = std::conditional_t<N <= 8, std::uint8_t,
                 std::conditional_t<N <= 16, std::uint16_t,
                 std::conditional_t<N <= 32, std::uint32_t,
                 std::conditional_t<N <= 64, std::uint64_t,
                 std::uintmax_t>>>>;
};

That seemed to do excactly what I need, how ever the practice looks different, since I can't compile it because of the following errors:

...Error: expected nested-name-specifier before 'type'
 using type = std::conditional_t<N <= 8, std::uint8_t,
       ^
...Error: using-declaration for non-member at class scope
...Error: expected ';' before '=' token
 using type = std::conditional_t<N <= 8, std::uint8_t,
            ^
...Error: expected unqualified-id before '=' token

I tried to google it, but none of the posts I found seem to adress this specific problems. Can anyone explain me what is wrong with this code? I'm pretty new to C++

like image 429
TreeOfLife Avatar asked Apr 10 '26 10:04

TreeOfLife


1 Answers

Your code is perfectly legal as long as your compiler supports c++14 stdlib's <type_traits> that define an alias template for std::conditional trait.

However, the error message clearly indicates you've not even enabled c++11, therefore using is not parsed as an alias at all. To do that, add -std=c++11 or -std=c++14 option to your configuration.

If you can't, then in c++03 you can easily implement your own conditional trait:

template <bool B, typename T, typename F>
struct conditional
{
    typedef T type;
};

template <typename T, typename F>
struct conditional<false, T, F>
{
    typedef F type;
};

template <int N>
struct int_type {
    typedef typename conditional<N <= 8, uint8_t,
                 typename conditional<N <= 16, uint16_t,
                 typename conditional<N <= 32, uint32_t,
                 typename conditional<N <= 64, uint64_t,
                 uintmax_t>::type>::type>::type>::type type;
};

In c++11 you can use std::conditional instead of std::conditional_t, the only difference is that you need to access a nested typedef type on your own, which is a dependent name (typename keyword needed in front of a nested name specifier):

#include <cstdint>
#include <type_traits>

template <int N>
struct int_type {
    using type = typename std::conditional<N <= 8, std::uint8_t,
                 typename std::conditional<N <= 16, std::uint16_t,
                 typename std::conditional<N <= 32, std::uint32_t,
                 typename std::conditional<N <= 64, std::uint64_t,
                 std::uintmax_t>::type>::type>::type>::type;
};

In c++11 and c++14, you need to include <cstdint> instead of <stdint.h>. The former guarantees the names are defined in the std namespace (the latter may define them only in the global namespace).

like image 81
Piotr Skotnicki Avatar answered Apr 12 '26 22:04

Piotr Skotnicki



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!