专用嵌套类的静态函数会导致编译器错误 C3855 (MSVC9)
Specializing A Nested Class' Static Function Results In Compiler Error C3855 (MSVC9)
这是我上一个问题的延续。
我正在尝试创建一个带有嵌套类模板的类模板,其中嵌套类模板有一个静态函数,我想为它提供专门化。
这是我的测试代码,它没有提供嵌套类的专门化。注意空类NullType
的存在——这就是我想要专门化嵌套类的地方。
#include <cstdlib>
#include <iostream>
using namespace std;
class NullType {};
template<class FooParam> struct Foo
{
template<class BarParam> struct Bar
{
static bool Magic();
};
};
template<class FooParam>
template<class BarParam>
bool Foo<FooParam>::Bar<BarParam>::Magic()
{
return true;
}
int main()
{
bool b = Foo<int>::Bar<int>::Magic();
cout << b << " ";
bool b2 = Foo<int>::Bar<NullType>::Magic();
cout << b2 << " ";
}
此代码按预期编译并运行。输出为:
1 1
现在我想在NullType
上提供Bar::Magic
的专门化,并让该函数返回false
。所需输出为:
1 0
所以我试着写这样的专业化:
template<class FooParam>
template<>
bool Foo<FooParam>::Bar<NullType>::Magic()
{
return false;
} // COMPILER ERROR HERE C3855
此定义出现在Foo::Bar::Magic()
的现有定义之前。
然而,此代码导致编译器错误:
1>.main.cpp(20) : error C3855: 'Foo<FooParam>::Bar<BarParam>': template parameter 'BarParam' is incompatible with the declaration
在上面指示的闭合支架上。
我可以使用什么语法来提供这种专门化?我对任何和所有技术都持开放态度,以实现我的主要目标(为NullType
返回false
,为其他一切返回true
(,只要:
- 没有使用外部库(没有Boost(
- 它只使用符合要求的C++
Foo
是类模板,Bar
是嵌套类模板或函数模板。返回false
或true
的函数必须是可特殊化或可重载的,这样调用NullType
的代码就会返回false
,而其他所有函数都会返回true
如果需要,我会澄清要求。
如@Ise Wisteria的评论中所述,C++03和C++11都禁止在不专门化其封闭类的情况下专门化嵌套类/方法。
以下使用重载的解决方案适用于您吗?
#include <cstdlib>
#include <iostream>
using namespace std;
class NullType {};
template<class FooParam> struct Foo
{
template<class BarParam> static bool Magic(BarParam);
static bool Magic(NullType);
};
template<class FooParam>
template<class BarParam>
bool Foo<FooParam>::Magic(BarParam)
{
return true;
}
template<class FooParam>
bool Foo<FooParam>::Magic(NullType)
{
return false;
}
int main()
{
bool b = Foo<int>::Magic(int());
cout << b << " ";
bool b2 = Foo<int>::Magic(NullType());
cout << b2 << " ";
}
另一个替代变体是使用非嵌套特征类:
class NullType {};
template<class FooParam> struct Foo
{
template<class BarParam> struct Bar
{
static bool Magic();
};
};
template<class T> struct bool_trait
{
static const bool value = true;
};
template<> struct bool_trait<NullType>
{
static const bool value = false;
};
template<class FooParam>
template<class BarParam>
bool Foo<FooParam>::Bar<BarParam>::Magic()
{
return bool_trait<BarParam>::value;
}
一个快速的解决方案是使用typeid运算符,这是标准C++(5.2.8(。因此,您的Magic((函数如下所示:
template<class FooParam>
template<class BarParam>
bool Foo<FooParam>::Bar<BarParam>::Magic()
{
if (typeid(BarParam) == typeid(NullType))
return false;
return true;
}
据我所知,编译器在typeid和typeinfo的实际实现中有一定的自由度,但==运算符的工作方式是可以保证的。需要注意的是,显然有些编译器为了性能考虑,默认不支持运行时类型信息;不过,大多数人都应该有一面旗帜来打开它。
相关文章:
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++,我收到一个无法理解的编译器错误
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 重载方法的方式会在使用临时调用时生成编译器错误
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- 有没有办法让编译器在我放置字符串而不是 nlohmann::json 对象时抛出错误?
- 致命错误 C1001 :vs2017 15.8.4 的内部错误(编译器文件"msc1.cpp",第
- C++2440错误-编译器认为字符串是常量字符
- Clang 错误 – 编译器错误或缺少一些细节?
- 视觉C++ 2017 错误?编译器优化表达式
- 配置:错误:C++编译器无法创建可执行文件
- PHP PDO_ODBC配置:错误:C 编译器无法创建可执行文件
- 致命错误 C1001:内部编译器错误(编译器文件"MSC1.cpp",第 1794 行)
- 错误:编译器限制达到简单hello world程序的堆限制
- c++链接错误:编译器找不到函数的定义
- 生成 CImg 时出现致命错误(编译器限制)
- 抛出对齐类型时出现 Clang 运行时错误.编译器错误