C++底漆和部分专业问题
在C ++ Primer Plus第14章的“部分专业”部分中:
没有部分专业化,第二个声明将使用一般模板,将t解释为类型char *。与部分专业化,它使用专业模板,将t解释为char。
部分专业化功能允许做出各种限制。例如,您可以使用以下内容://常规模板
模板<类T1,T2类,T3类>类三重奏{...};// T3设置为T2
专业化 模板<类T1类,T2>三人组< t1,t2,t2> {...};//用T3和T2设置为T1*
专业化 模板类三重奏< t1,t1*,t1*> {...};鉴于这些声明,编译器将做出以下选择:
trio< int,简称,char *> T1; //使用常规模板
trio< int,简短> T2; //使用三重奏< t1,t2,t2>
trio< char,char *,char *> T3; //使用三重奏< t1,t1*,t1*>
但是,当我尝试一下时:
template <typename T1, typename T2, typename T3>
class Base
{
public:
Base()
{
cout << "general definition" << endl;
}
};
template <typename T1, typename T2>
class Base<T1, T2, T2>
{
public:
Base()
{
cout << "T1 and T2 definition" << endl;
}
};
template <typename T1>
class Base<T1, T1, T1>
{
public:
Base()
{
cout << "T1 definition" << endl;
}
};
int main()
{
Base<int, char, double> b1;
Base<int, char, char> b2;
Base<int, char> b3;
Base<int, int, int> b4;
Base<int> b5;
return 0;
}
我会在base&lt; int,char&gt;中得到错误。 B3
和base&lt; int&gt; B5
:
wrong number of template arguments (2, should be 3)
wrong number of template arguments (1, should be 3)
我的问题是:当我们使用部分专业化时,如果使用类型参数来初始化另一个类型,我们可以省略重复零件吗?
我的编译器参数:
D:\MinGw-x64\mingw64\bin\g++.exe 'c:\Users\33065\Desktop\study\C++ Primer Plus source code\*.cpp', '-o', 'c:\Users\33065\Desktop\study\C++ Primer Plus source code/main.exe', '-std=c++11', '-g', '-m64', '-Wall', '-static-libgcc', '-fexec-charset=GBK', '-D__USE_MINGW_ANSI_STDIO'
In the "Partial Specializations" section of chapter 14 of C++ Primer Plus:
Without the partial specialization, the second declaration would use the general template, interpreting T as type char *.With the partial specialization, it uses the specialized template, interpreting T as char.
The partial specialization feature allows for making a variety of restrictions. For example, you can use the following:// general template
template <class T1, class T2, class T3> class Trio{...};// specialization with T3 set to T2
template <class T1, class T2> class Trio<T1, T2, T2> {...};// specialization with T3 and T2 set to T1*
template class Trio<T1, T1*, T1*> {...};Given these declarations, the compiler would make the following choices:
Trio<int, short, char *> t1; // use general template
Trio<int, short> t2; // use Trio<T1, T2, T2>
Trio<char, char *, char *> t3; // use Trio<T1, T1*, T1*>
But when I try this:
template <typename T1, typename T2, typename T3>
class Base
{
public:
Base()
{
cout << "general definition" << endl;
}
};
template <typename T1, typename T2>
class Base<T1, T2, T2>
{
public:
Base()
{
cout << "T1 and T2 definition" << endl;
}
};
template <typename T1>
class Base<T1, T1, T1>
{
public:
Base()
{
cout << "T1 definition" << endl;
}
};
int main()
{
Base<int, char, double> b1;
Base<int, char, char> b2;
Base<int, char> b3;
Base<int, int, int> b4;
Base<int> b5;
return 0;
}
I get errors in Base<int, char> b3
and Base<int> b5
:
wrong number of template arguments (2, should be 3)
wrong number of template arguments (1, should be 3)
My question is: When we use partial specializations, if we use a type parameter to initialize another, can we omitting the repetitive part?
My compiler parameters:
D:\MinGw-x64\mingw64\bin\g++.exe 'c:\Users\33065\Desktop\study\C++ Primer Plus source code\*.cpp', '-o', 'c:\Users\33065\Desktop\study\C++ Primer Plus source code/main.exe', '-std=c++11', '-g', '-m64', '-Wall', '-static-libgcc', '-fexec-charset=GBK', '-D__USE_MINGW_ANSI_STDIO'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
部分专业化将期望与基本模板授权一样多的模板参数(“基础”表示您的类模板的无专门声明,而不是名为
base
的类型)。解决问题的一种解决方案是将基本模板定义为:由于专业人士无法具有默认的模板参数,因此这会使基本模板将未定义的参数滴入到其余的打字员。因此,您可以写出类似的内容,
并使每种未定义的类型等同于上一个类型。
demo
A partial specialization will expect as many template arguments as the base template mandates ("base" meaning the unspecialized declaration of your class template, NOT the type named
Base
). A solution to your problem would be to define the base template as:Since specializations can't have defaulted template arguments, this makes the base template trickle down non-defined parameters to the rest of the typelist. Hence you can write things like:
and have each undefined type equate to the previous type.
Demo