C++模板专用化:编译错误:"is not a type"
C++ Template Specialization: compile error: "is not a type"
如果我删除模板专用化部分(尝试打印"Test 2"的部分),代码可以很好地编译,但我希望能够有一个特殊情况,运行不同的代码路径,对外部用户来说看起来很干净。
#include <iostream>
using namespace std;
struct SpecialType {};
template<typename A , typename B = SpecialType>
class Test
{
public:
class TestInner
{
public:
TestInner& operator* ();
};
};
template<typename A , typename B>
typename Test<A , B>::TestInner& Test<A , B>::TestInner::operator* ()
{
cout << "Test 1" << endl;
return *this;
}
// If the following is removed, everything compiles/works, but I want this alternate code path:
template<typename A>
typename Test<A , SpecialType>::TestInner& Test<A , SpecialType>::TestInner::operator* ()
{
cout << "Test 2" << endl;
return *this;
}
int main()
{
Test<int , SpecialType>::TestInner test;
*test;
return 0;
}
我做错了什么?
编辑:顺便说一下,编译器错误如下:
main.cpp:26:44: error: 'Test<A, SpecialType>::TestInner' is not a type
typename Test<A , SpecialType>::TestInner& Test<A , SpecialType>::TestInner::operator* ()
^
main.cpp:26:89: error: invalid use of dependent type 'typename Test<A, SpecialType>::TestInner'
typename Test<A , SpecialType>::TestInner& Test<A , SpecialType>::TestInner::operator* ()
^
为专业类添加声明:
template<typename A>
class Test<A, SpecialType>
{
public:
class TestInner
{
public:
TestInner& operator* ();
};
};
问题是您为未声明的专用化定义了一个成员。模板化类的专用化不与通用模板共享任何成员或方法,因此通用模板的声明不用作该模板类的任何专用化的声明。
考虑一下:
template <class T>
class Foo {
void GeneralFunction(T x);
}
和专业化:
template <>
class Foo<int> {
void SpecialisedFunction(int x);
}
在这里,Foo</*anything except int*/>
只有方法GeneralFunction
而Foo<int>
只有方法SpecialisedFunction
。
按照同样的逻辑,这也是允许的:
template<>
class Foo<float> {
float GeneralFunction; //here GeneralFunction is a data member, not a method.
}
长话短说,您需要声明您的专业化。
相关文章:
- Directx 11 - CompileFromFile() is not compiling
- Centos7 g++ "to_string is not in a member of std"
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- reference_wrapper导致"incomplete type is not allowed"
- 结构中的错误"Incomplete type is not allowed"
- "Called Object Type is Not a Function or Pointer" 与 typedef 和类
- 无法创建 DLL:获取 DLL "is not a valid Win32 application"
- Atom gpp编译器给出"'C:UsersadminUser' is not recognized as an internal or..."错误
- 当我编译XG-Bosst时,出现错误"error: 'Split' is not a member of 'xgboost::common' ……"
- C++ DLL 运行时错误"abc.dll is not a valid WIN32 application" 。请帮助解决这个问题
- 如何修复 eigen3 中的'non-type template argument is not a constant expression'?
- 我的代码应该接受一个数字,并返回字母等级或"Grade is not valid"但 else 语句不起作用
- 如何修复此错误"the value of 'x1' is not usable in a constant expression"?static_assert
- 加载安全区图像"A device attached to the system is not functioning"
- 巴泽尔中的错误"name 'new_local_repository' is not defined"
- 如何修复 SDL2 "GLSL 3.30 is not supported"错误
- 函数模板(它是类模板的成员)的显式专用化会产生"partial specialization is not allowed"错误,为什么?
- 为什么"an inherited constructor is not a candidate for initialization from an expression of the same or
- CGAL 绘制函数在 Visual Studio 中给出'CGAL_USE_BASIC_VIEWER is not defined'错误
- "OpenCV Error: The function/feature is not implemented"安卓应用程序