以下代码:
template <class T1>
struct A1
{
template <int INDEX>
struct A2 { /* ... */ };
template <>
struct A2<-1> { /* ... */ };
};
int main()
{
A1<int>::A2<-1> x;
}
Run Code Online (Sandbox Code Playgroud)
给出了这个错误:
prog.cpp:7:13:错误:非命名空间范围的显式特化
'struct A1<T1>'prog.cpp:8:10:错误:部分特化中未使用的模板参数:
prog.cpp:8:10:错误:'T1'
如何最好地解决此错误?我试过这个:
template <class T1>
struct A1
{
template <int INDEX, class DUMMY = void>
struct A2 { /* ... */ };
template <class DUMMY>
struct A2<-1, DUMMY> { /* ... */ };
};
int main()
{
A1<int>::A2<-1> x;
}
Run Code Online (Sandbox Code Playgroud)
这似乎工作,但似乎有点软糖. …
这是代码:
template <typename T>
struct A
{
template <typename U>
struct B;
};
template <typename T> template <> // 0_o
struct A<T>::B<int> {};
Run Code Online (Sandbox Code Playgroud)
我知道我不能这样做,但我更有兴趣知道逻辑上为什么我不能专门化嵌套模板成员而不专门封装类模板?
我很感激任何有关逻辑解释的帮助:)
Andrei Alexandrescu的回答:"没有特别的理由 - 这只是一种语言规则."
在找到关于stackoverflow的许多问题的答案之后,我现在遇到了一个我无法找到答案的问题,我希望有人愿意帮助我!
我的问题是我想在C++中对一个类中的函数进行明确的模板化.我的编译器(g ++)和C++标准(§14.7.3)中的一个看起来告诉我,这个特化必须在声明类的命名空间中完成.我明白这意味着我不能把专业化放在课堂里,但是我没有看到这个限制的重点!有没有人知道是否有充分的理由不让专业在课堂上进行?
我知道有一些解决方法,例如将函数放在结构体中,但我想理解为什么语言有这种设计.如果有充分的理由不在课堂上允许专门的功能,我想在尝试解决它之前我应该知道它.
提前致谢!
为了让我的问题更加精确:以下是一些测试示例中的代码,说明了我想要做的事情:
#include <cstdio>
namespace MalinTester {
template <size_t DIMENSIONALITY>
class SpecializationTest {
public:
SpecializationTest() {
privateVariable = 5;
};
virtual ~SpecializationTest() {};
void execute() {
execute<DIMENSIONALITY>();
};
private:
int privateVariable;
template <size_t currentDim>
static void execute() {
printf("This is the general case. Current dim is %d. The private variable is %d.\n", currentDim, privateVariable);
execute<currentDim-1>();
}
template <>
static void execute<0>() {
printf("This is the base case. Current dim is 0.\n");
}
};
Run Code Online (Sandbox Code Playgroud)
这是不可能的; g ++说: …
我有一个嵌套在另一个模板中的类模板.部分特殊化很简单:我只是template< … >在其父级中声明另一个块.
但是,我需要另一个部分特化,恰好指定其所有本地模板参数.这使它成为一个明确的专业化.无论出于何种原因,显式特化都必须在命名空间范围内.要在父类之外声明它,必须提名父级,这需要非空的模板参数列表.这意味着部分专业化.部分专业化是我正在做的事情,它应该在任意外部范围内工作.但GCC和Comeau都无法使用部分特化形式参数识别父提名中的模板参数.
template< class X > struct A {
template< class Y > struct B; // initial declaration OK
template< class Z >
struct B< A< Z > > {}; // partial OK as long as there's a local arg
template<> // ERROR: this syntax triggers explicit specialization
struct B< int > {};
};
template<> // ERROR: can't nest template<>s here (why?)
template< class X > // ERROR: can't deduce X from type of …Run Code Online (Sandbox Code Playgroud) 我试图创建一个模板化类的部分专用成员,其中内部类的模板类型来自外部类...以下:
template<typename T>
struct Num
{
template <T n>
struct VH
{
enum { value = n };
T v = value;
};
};
template <typename T> struct Num<T>::VH<0>
{
enum {value = 1};
T v = value;
};
template <typename T> struct Num<T>::VH<1>
{
enum {value = 0};
T v = value;
};
Run Code Online (Sandbox Code Playgroud)
失败了
error: too few template-parameter-lists
template <typename T> struct Num<T>::VH<0>
Run Code Online (Sandbox Code Playgroud)
以下内容:
template <typename T> struct Num<T>::template<> VH<0>
{
enum {value = 0};
T v = value; …Run Code Online (Sandbox Code Playgroud)