简体   繁体   English

C++ 中的“使用类型”会导致几个错误

[英]'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:在我的问题类型为 c++ 中的 returntype 中,我得到了一个答案,该答案给了我这样的结构:

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++我对 C++ 很陌生

Your code is perfectly legal as long as your compiler supports stdlib's <type_traits> that define an alias template for std::conditional trait.只要您的编译器支持 stdlib 的<type_traits>定义std::conditional特征的别名模板,您的代码就是完全合法的。

However, the error message clearly indicates you've not even enabled , therefore using is not parsed as an alias at all.但是,错误消息清楚地表明您甚至没有启用 ,因此using根本不会被解析为别名。 To do that, add -std=c++11 or -std=c++14 option to your configuration.为此,请将-std=c++11-std=c++14选项添加到您的配置中。

If you can't, then in you can easily implement your own conditional 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 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 ):你可以使用std::conditional而不是std::conditional_t ,唯一的区别是你需要自己访问一个嵌套的 typedef type ,它是一个依赖名称(在嵌套的前面需要typename关键字名称说明符):

#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 and , you need to include <cstdint> instead of <stdint.h> .,您需要包含<cstdint>而不是<stdint.h> The former guarantees the names are defined in the std namespace (the latter may define them only in the global namespace).前者保证名称在std命名空间中定义(后者只能在全局命名空间中定义它们)。

I'll add an answer here for the sake of completeness.为了完整起见,我将在这里添加一个答案。

You need to #include <cstdint> and #include <type_traits> , then compile with C++14 support.您需要#include <cstdint>#include <type_traits> ,然后使用 C++14 支持进行编译。 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现场示例: http : //coliru.stacked-crooked.com/a/9352f3349f48bff4

Huh, when you do a typedef the compiler tells you what's wrong.嗯,当你做一个 typedef 时,编译器会告诉你哪里出了问题。 Those are dependant typenames so you need a "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;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM