在编写特征以进行参考和非参考类型时,如何减少重复
How to reduce duplication when writing traits for reference and non reference types when the traits are the same
我有例如
#include <iostream>
template <typename T>
struct Base {};
template <>
struct Base<std::string> {
static const int value = true;
};
template <>
struct Base<std::string &> {
static const int value = true;
};
int main() {
bool a = Base<std::string>::value;
bool b = Base<std::string &>::value;
std::cout << a << b << std::endl;
}
https://godbolt.org/z/0npyxb
注意,我有两个相同的专业,并希望将其简化为一个。我知道有两种解决方案我不想做。
(1(在呼叫站点上删除参考,以便仅需要一个专业化。
(2(创建一个基类并继承参考和 no thit。 是否有第三个选项,其中专业化是普通的,而不是参考类型? C 11解决方案。
1(似乎很好:
template <typename T>
struct BaseImpl {};
template <>
struct BaseImpl<std::string> {
static const int value = true;
};
template <typename T>
using Base = BaseImpl<typename std::remove_reference<T>::type>;
2(似乎有更多的详细信息
template <typename T>
struct BaseImpl {};
template <>
struct BaseImpl<std::string> {
static const int value = true;
};
template <typename T>
struct Base : BaseImpl<T> {}; // or directly BaseImpl<std::remove_reference_t<T>>
template <typename T>
struct Base<T&> : BaseImpl<T> {};
3(类似于2(,更少的冗长,但可能更棘手
template <typename T>
struct Base : Base<T&> {};
template <typename T>
struct Base<T&> {};
template <>
struct Base : Base<std::string> {
static const int value = true;
};
1(似乎更易读,易于实现。
您可以在Sfinae上下文中执行检查:
// type trait to remove the lvalue-reference
template< class T > struct remove_lvalue_reference {typedef T type;};
template< class T > struct remove_lvalue_reference<T&> {typedef T type;};
template <typename T>
using remove_lvalue_reference_t = typename remove_lvalue_reference<T>::type;
template <typename T, typename = void>
struct Base {};
// valid when T is std::string and std::string&
template <typename T>
struct Base<T, typename std::enable_if<std::is_same<std::string, remove_lvalue_reference_t<T>>::value>::type> {
static const int value = true;
};
live
enable_if有点沉重,但我认为没有更好的方法。
#include <iostream>
#include <type_traits>
template <typename T, typename Enable = void>
struct Base {};
template <typename T>
struct Base<
T,
typename std::enable_if<
std::is_same<typename std::decay<T>::type, std::string>::value
>::type
>
{
static const int value = true;
};
int main() {
bool a = Base<std::string>::value;
bool b = Base<std::string &>::value;
std::cout << a << b << std::endl;
}
https://godbolt.org/z/98vzfn
相关文章:
- 为什么"fun(i)"被推导出为"fun<int&>"而不是"fun<int>",因为"i"是"int"的类型而不是参考?
- 不能使这种类型的"void(C::* volatile)(int) const "在参考手册C++示
- 返回int数据类型v/s传递参考
- 将参考类型作为嵌套模板结构中的模板参数作为模板参数不起作用
- 如何检查两种类型是否相同,忽略const和参考
- CPP中的单个参考代表两种或多个数据类型
- const自动参考绑定到(NULL)指针 - 实际类型是什么
- 维护对元素参考类型
- 为什么可以将其他类型变量用作C 中常量参考参数参数的参数
- 在编写特征以进行参考和非参考类型时,如何减少重复
- Python ctypes,回调函数增强类型是C++参考
- 通过参考推断模板包中的冲突类型
- 如何使用自我参考类型并在C 类中使用别名
- 如何在基本类型参考中存储对派生类型的参考
- 试图分配输出以查看指针和参考输出时,请勿命名类型错误
- 了解普遍参考的类型扣除
- sizeof参考对于类型和数据成员的不同
- 参考类型静态数据成员的问题可能是编译器错误
- 比较C 中的自动和参考类型
- 初始化C 的参考类型