我有一个奇怪的编译警告,使用Visual C++ 2010编译以下代码:
#include <iostream>
class test
{
public:
template<class obj>
class inner
{
private:
// Line 11:
template<int index, bool unused = true> struct AttributeName;
private:
template<bool b>
struct AttributeName<0,b>
{
static inline const char* get()
{
return "prop";
}
};
public:
typedef AttributeName<0> propname;
};
typedef inner<test> description;
};
int main()
{
test t;
std::cout << test::description::propname::get(); // Line 32
return 0;
}
警告:
file.cpp(11) : warning C4348: 'test::inner<obj>::AttributeName' : redefinition of default parameter : parameter 2 (with [ obj=test ])
file.cpp(11) : see declaration of 'test::inner<obj>::AttributeName' (with [ obj=test ])
file.cpp(32) : see reference to class template instantiation 'test::inner<obj>' being compiled (with [ obj=test ])
我不理解的是,
AttributeName
的“重新定义”与定义在同一行...听起来像是一个错误。我注意到将
inner
变为非模板类可以消除警告。然而,这不是一个选项,因为真正的代码比这个测试用例更复杂并且需要进行模板化。此外,如果将警告视为错误,则此代码将无法编译...
在GCC上编译时没有警告。
为什么msvc会输出这样的警告,并且有没有解决方法? 编辑 以下修改:
template<int index, bool unused = true> struct AttributeName {};
似乎已经消除了警告。