C++ 关于别名模板

C++ 关于别名模板,c++,c++11,templates,C++,C++11,Templates,模板 结构乐趣; 样板 结构乐趣{ 使用type=std::add_lvalue_reference的模板; }; 样板 结构乐趣{ 使用type=std::remove_引用的模板; }; 样板 样板 使用Fun=typename Fun\:模板类型; //问题1。我改变了两个模板的位置,并且得到了一个编译错误。所以在这种情况下我真的无法区分两个模板的顺序。有介绍吗? //模板 //模板 //使用Fun=typename Fun\:模板类型; 使用RomoveRef=Fun的模板; int m

模板
结构乐趣;
样板
结构乐趣{
使用type=std::add_lvalue_reference的模板;
};
样板
结构乐趣{
使用type=std::remove_引用的模板;
};
样板
样板
使用Fun=typename Fun\:模板类型;
//问题1。我改变了两个模板的位置,并且得到了一个编译错误。所以在这种情况下我真的无法区分两个模板的顺序。有介绍吗?
//模板
//模板
//使用Fun=typename Fun\:模板类型;
使用RomoveRef=Fun的模板;
int main()
{
RomoveRef::类型j=1;//确定
printf(“%d\n”,j);
//问题2.我想直接使用乐趣,我该怎么做?
//模板乐趣::类型i=1;
//printf(“%d\n”,i);
返回0;
}
我有两个问题写在上面代码的注释部分。如果可能的话,请给我一些建议,谢谢

1.如何理解两个模板的位置。
2.如何使用Fun::type或Fun::type实现与RomoveRef相同的功能关于第一个问题,
g++
说“参数列表太多”,其中
clang++
说“别名模板声明中的无关模板参数列表”。 要使代码编译,您应该编写以下代码:

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;
}
模板
使用Fun=typename Fun\:模板类型;
关于第二个函数,如果我理解正确,也许你想要

template <bool AddOrRemove, typename T>
using Fun = typename Fun_<AddOrRemove>::template type<T>;
模板
使用RomoveRef=Fun::value,T>;
template <typename T>
using RomoveRef = Fun<!std::is_reference<T>::value, T>;