'using type' in C ++ throws multiple errors

In my question, type returntype in C ++ I was provided with an answer that gave me a structure like this:

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>>>>;
};

      

It seemed like this was really what I needed, how the practice would look different as I cannot compile it due to 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 this, but none of the posts I found addressed these specific issues. Can anyone explain to me what is wrong with this code? I am new to C ++

+3


source to share


3 answers


Your code is perfectly legal as long as your compiler supports C ++ 14 stdlibs <type_traits>

that define an alias pattern for std::conditional

.

However, the error message clearly indicates that you haven't even included C ++ 11, so it is using

not parsed as an alias at all. To do this, add to the configuration -std=c++11

or -std=c++14

.

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

:

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 std::conditional_t

, the only difference is you need to access the nested typedef type

yourself, which is the dependent name (required typename

) before the 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 / C ++ 14, you need to include <cstdint>

instead <stdint.h>

. The former ensures that names are defined in the namespace std

(the latter can only define them in the global namespace).

+2


source


I'll add an answer here for completeness.

You need #include <cstdint>

and #include <type_traits>

and then compile with C ++ 14 support. Code:



#include <type_traits>
#include <cstdint>
#include <iostream>

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>>>>;
};

int main()
{
    int_type<10>::type t; // std::uint16_t
    t = 12;
    std::cout << t << std::endl;
}

      

Live example: http://coliru.stacked-crooked.com/a/9352f3349f48bff4

0


source


Yes, when you do a typedef, the compiler tells you what's wrong. These are dependent names, so you need "typename" in front of it. so this should work:

#include <iostream>
#include <type_traits>
using namespace std;

template <int N>
struct int_type {
    using mytype = 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;
};

      

-1


source







All Articles