关于模板别名
About template alias
给定以下代码 2 个问题:
template <bool AddOrRemoveRef>
struct Fun_;
template <>
struct Fun_<true> {
template <typename T> using type = std::add_lvalue_reference<T>;
};
template <>
struct Fun_<false> {
template <typename T> using type = std::remove_reference<T>;
};
template <typename T>
template<bool AddOrRemove>
using Fun = typename Fun_<AddOrRemove>:: template type<T>;
template <typename T> using RomoveRef = Fun<false>;
int main()
{
RomoveRef<int&>::type j = 1; // ok
printf("%dn", j);
// question 2. I want to use Fun directly, how can i do?
// template Fun<false>::type<int&> i = 1;
// printf("%dn", i);
return 0;
}
1.如何理解两种template <>
立场?
2.如何使用Fun::type
或Fun_::type
实现与RomoveRef
相同的功能?
关于第一个问题,g++
说"参数列表太多",其中clang++
说"别名模板声明中的无关模板参数列表"。要使代码编译,您应该编写以下内容:
template <bool AddOrRemove, typename T>
using Fun = typename Fun_<AddOrRemove>::template type<T>;
关于第二个功能,如果我理解正确,也许你想要类似的东西
template <typename T>
using RomoveRef = Fun<!std::is_reference<T>::value, T>;
相关文章:
- 使用模板参数重载C++方法:如何使其适用于模板的子类?
- 如何使用类型别名从模板化类中隐藏模板列表
- 类成员函数参数列表是否可以依赖于模板参数?
- 依赖于模板的错误
- 将 c++ 类成员函数专用于模板类
- C++ 使函数调用依赖于模板参数
- C++别名的模板参数包扩展
- c ++如何将模板应用于模板类的孩子/朋友?
- 如何修复"没有依赖于模板参数的参数'glGenVertexArrays'......"C++ 中的错误
- 如何转发声明依赖于变量定义的类,而变体定义又依赖于模板化类?
- 别名一个模板函数,该功能没有参数可更简洁
- 类型别名和模板模板参数推导
- 混合别名和模板专业
- 动态强制转换适用于模板成员函数内的共享指针和弱指针,无需代码重复
- 如何确保 "<<" 运算符适用于模板化 ADT 定义中的任何泛型类型?
- 将 const 限定符应用于模板参数时的不同编译器行为
- 如何定义依赖于模板参数的类型定义
- 如何使用类型专用化模板方法,该类型本身就是一个模板,其中只有返回类型依赖于模板类型
- PHP 相当于C++模板
- 使用的类型别名如何指定依赖于模板参数的模板模板参数