- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
如何在 C++11 中编写自定义元编程测试?我想写这样的东西:
#include <type_traits>
#include <iostream>
struct A {};
template <typename T>
struct foo {
typedef typename std::conditional<std::is_pointer<T>::value,
typename std::remove_pointer<T>::type,
T>::type type;
};
template <typename A, typename B>
struct test1{typedef typename std::is_same<A, B>::value result;};
template <typename A, typename B>
struct test2{typedef typename std::is_same<A, typename foo<B>::type>::value result;};
template <typename A, typename B>
void testAll() {
std::cout << std::boolalpha;
std::cout << "test1: " << typename test1<A,B>::result << std::endl; // ERROR: expected ‘(’ before ‘<<’ token
std::cout << "test2: " << typename test2<A,B>::result << std::endl; // ERROR: expected ‘(’ before ‘<<’ token
// ...
}
int main()
{
typedef A type1;
testAll<A, type1>();
typedef const A* type2;
testAll<A, type2>();
// ...
}
我从 here 看到了一个可能的 is_same 实现.我需要这样的东西吗?
可以这么写:
std::cout << "test1: " << std::is_same<A, B>::value << std::endl;
我想这样写:
std::cout << "test1: " << test1<A, B>::result << std::endl;
最佳答案
您正在使用 typename
之前 test1<A,B>::result
,但这是不合适的,因为你想要 result
成为一个值,而不是一个类型。出于同样的原因,你不应该将它定义为 test1<>
中的类型别名。 : 您只是希望它具有由 std::is_same<>::value
返回的相同值 (这是一个 static const bool
成员变量,而不是类型名称)。
你可以这样写:
template <typename A, typename B>
struct test1
{
static const bool result = std::is_same<A, B>::value;
};
这样下面的行将被编译:
std::cout << "test1: " << test1<A,B>::result << std::endl;
但是,您的 test1<>
trait 只不过是 std::is_same<>
的别名而已(使用 result
而不是 value
),并且 C++11 支持别名模板:
template <typename A, typename B>
using test1 = std::is_same<A, B>;
这将使您能够:
std::cout << "test1: " << test1<A,B>::value << std::endl;
test2<>
trait 也有类似的问题,因为它定义了 result
作为类型别名,但是 std::is_same<A, typename foo<B>::type>::value
是一个值,而不是一个类型。
因此您可以再次将其重写如下:
template <typename A, typename B>
struct test2
{
static const bool result = std::is_same<A, typename foo<B>::type>::value;
};
这样下面的行将被编译:
std::cout << "test2: " << test2<A, B>::result << std::endl;
但同样,您也可以定义一个别名模板:
template <typename A, typename B>
using test2 = std::is_same<A, typename foo<B>::type>;
关于c++ - 如何使用 C++11 type_traits 创建自定义元编程测试结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15393746/
我安装了 Qt 5.8,但出现此错误。我的 pro 文件中也有 CONFIG += c++14,我也尝试过使用 c++11。 /usr/include/x86_64-linux-gnu/qt5/QtC
我正在尝试构建 PrintFunctionNames来自 clang 的示例。但是我收到以下错误: [mac-osx:clang/examples/PrintFunctionNames] osx% c
我在 头文件中看到了一些实现,但是有些实现我找不到,像这样: // STRUCT TEMPLATE is_class template struct is_class : bool_constant
我有一个处理任何容器类型的接口(interface)。 std::vector、std::array,甚至 std::basic_string。问题是没有什么可以阻止某人传递没有连续内存的容器。 我目
看过this answer ,我尝试为其中的代码提供一个变量模板实用程序: template class Template> struct is_specialization : std::fals
我有一个函数“has_holes”,它应该根据掩码计算一些东西。位数由“掩码”的类型决定。因此我想使用模板。此外,我只想允许 has_holes 的实例化,它按值获取参数。所以我添加了一个类型特征“r
在 C++11 及更高版本中, header 包含很多类型检查的类,比如 std::is_empty , std::is_polymorphic , std::is_trivially_constr
我想让用户控制基类的“内部类型”。此代码工作正常。 版本 1 ( demo ) //library layer template class BT_trait{ public: using t
我想知道包含 type_traits 是否真的值得 header 只是为了获取枚举的基础类型。我正在创建一个 Flags 类,我希望它尽可能灵活,所以我使用 std::underlying_type:
我正在尝试从一个类似于下面所示的类中合并许多非常相似的函数方法,我认为有效实现它的最佳方法是通过使用模板结合模板函数特化或替代类型特征。我是模板特化和类型特征的新手,但我了解基本概念,这就是为什么我要
我正在编写一个模板类,其中包含一个执行某些按位运算的方法,因此我想限制类型,以防在 is_integral 中使用此方法。我举了一个简单的例子 here并修改如下: #include #includ
我已经创建了一个可变参数模板函数,它根据模板参数的类型执行不同的操作,在此不赘述。我将实现简化为一个非常简单的控制台打印示例: #include #include #include #inclu
是否可以根据 type_traits 信息重载函数/类模板? 例子: #include template class object_worker { public: object_worke
如何从此 operator+() 返回 std::vector? #include #include #include #include #include template struct i
我在看最新的C9 lecture并注意到一些有趣的事情.. 在他对 type_traits 的介绍中,Stephan 使用了以下(如他所说,人为的)示例: template void foo(T t
是否有可能使用类型删除来创建封装任意类型的对象(我们称之为 ErasedType ),并且可以在运行时查询以判断是否存在另一个任意类型 T可转换为 ErasedType ? 考虑之后,我不认为这是可能
我一直在尝试浏览 Boost type-traits header ,考虑到无数#define 提供的强烈不可读性,现在我感到非常恶心。然后是更多#define。 具体来说,我有兴趣弄清楚以下 3 个
讨论 根据标准§20.10.2/1 Header 概要 [meta.type.synop]: 1 The behavior of a program that adds specializations
我有以下情况:我的问题围绕使用强类型枚举类作为标志(就像在 C# 中使用 Flags-Attribute 一样)。我知道这不是首先要使用枚举类的方式,但这不是这个问题的重点。 我已经定义了几个用于这些
背景 我正在尝试写一个 class template Hasher这将以两种不同的方式实现,具体取决于是否 std::hash已为 T 实现: template struct Hasher { s
我是一名优秀的程序员,十分优秀!