如何在 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>();
// ...
}
我从这里看到了一个可能的 is_same 实现。我需要这样的东西吗?
可以这样写:
std::cout << "test1: " << std::is_same<A, B>::value << std::endl;
我想写这个:
std::cout << "test1: " << test1<A, B>::result << std::endl;