![](/img/trans.png)
[英]c++ using namespace std causes errors with bind () and a waiver with the iterator
[英]'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++ 很陌生
只要您的编译器支持c++14 stdlib 的<type_traits>
定义std::conditional
特征的别名模板,您的代码就是完全合法的。
但是,错误消息清楚地表明您甚至没有启用c++11 ,因此using
根本不会被解析为别名。 为此,请将-std=c++11
或-std=c++14
选项添加到您的配置中。
如果不能,那么在c++03 中,您可以轻松实现自己的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;
};
在c++11 中,你可以使用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;
};
在c++11和c++14 中,您需要包含<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.