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>;
// question 1. I changed the two template <> postion,and got a compile error.So i really can not distinguish the sequence of two template <T> in this situation. Is there some introduction?
// template <bool AddOrRemove>
// template <typename T>
// using Fun = typename Fun_<AddOrRemove>:: template type<T>;
template <typename T> using RomoveRef = Fun<false>;
int main()
{
RomoveRef<int&>::type j = 1; // ok
printf("%d\n", j);
// question 2. I want to use Fun directly, how can i do?
// template Fun<false>::type<int&> i = 1;
// printf("%d\n", i);
return 0;
}
我有两个问题写在上面代码的注释部分,如果可以的话请给我一些建议,谢谢。
1.如何理解两个模板<>的位置。 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++ - 关于别名模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54008920/