簡體   English   中英

C++ 中的“使用類型”會導致幾個錯誤

[英]'using type' in C++ causes several errors

在我的問題類型為 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>>>>;
};

這似乎完全符合我的需要,無論實踐看起來如何不同,因為由於以下錯誤我無法編譯它:

...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

我試着用谷歌搜索,但我發現的帖子似乎都沒有解決這個特定的問題。 誰能解釋一下這段代碼有什么問題? 我對 C++ 很陌生

只要您的編譯器支持 stdlib 的<type_traits>定義std::conditional特征的別名模板,您的代碼就是完全合法的。

但是,錯誤消息清楚地表明您甚至沒有啟用 ,因此using根本不會被解析為別名。 為此,請將-std=c++11-std=c++14選項添加到您的配置中。

如果不能,那么在您可以輕松實現自己的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;
};

你可以使用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;
};

,您需要包含<cstdint>而不是<stdint.h> 前者保證名稱在std命名空間中定義(后者只能在全局命名空間中定義它們)。

為了完整起見,我將在這里添加一個答案。

您需要#include <cstdint>#include <type_traits> ,然后使用 C++14 支持進行編譯。 代碼:

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

現場示例: http : //coliru.stacked-crooked.com/a/9352f3349f48bff4

嗯,當你做一個 typedef 時,編譯器會告訴你哪里出了問題。 這些是依賴的類型名,所以你需要在它前面加上一個“類型名”。 所以這應該有效:

#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