我可以在 VS 2010 (with Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
) 中重现它,我相信这是一个错误。说实话,我很难理解这里涉及的范围问题,但这似乎与手头的问题无关(因为基本模板和专业化嵌套在一起,VS 抱怨递归级别,而不是未定义的符号)。无论如何,这是一个非常有趣的例子。我在 VS 2005 中也看到了相同的行为(带有Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86
)。似乎微软仍然不接受关于 VS 2010 的错误报告(从Microsoft Connect Visual Studio 页面和反馈表来看))。希望有权访问 VS 2012 的人能够测试您的原始代码并在问题尚未解决时提交错误报告(我认为这很可能,我没有看到任何迹象表明它之前已引起 Microsoft 的注意)。
要解决此问题,请前向声明导致递归的类模板:
// Forward declaration of problematic inherited class template
template<template<int> class A, int n = 0, typename obj_type = typename A<n>::type>
struct Instantiate;
template<template<int> class A, int n>
struct Instantiate<A,n,void>
{
};
// Now compiles!
template<typename O>
struct test
{
template<int n, bool=true> struct array { typedef void type; };
template<int n> struct array2 { typedef typename array<n>::type type; };
template<bool u> struct array<0,u> { typedef int type; };
template<bool u> struct array<1,u> { typedef float type; };
template<bool u> struct array<2,u> { typedef bool type; };
Instantiate<array2> objects;
};
// Instantiate each type of a type array (which is a template<int>)
// void is taken as the end of the array
template<template<int> class A, int n, typename obj_type>
struct Instantiate : public Instantiate<A,n+1>
{
obj_type object;
Instantiate() : object() {}
};
int main()
{
test<int> obj;
return 0;
}
编辑
Synxis,感谢rise4fun 链接,以后肯定会派上用场。令人惊奇的是,虽然您的原始代码可以正常工作VS2012 CTP
,但它仍然存在问题VS2012 RTM
!见http://rise4fun.com/Vcpp/aRh。为什么CTP
是默认选项而不是RTM
超出我的范围。这个例子绝对是一个奇怪的例子。
除了你的库头文件之外,还有其他东西Instantiate
被使用test
吗?如果没有,用户可以显式转发声明Instantiate
模板定义test
,然后包含您的标题。否则,您可以创建一个头文件,其中包含前向声明以及所需的其他内容。您很可能希望为受此问题影响的用户创建特殊的解决方法标头。这肯定会让事情变得非常混乱和容易出错(主要是由于默认的模板参数),但它会起作用:
myLib_workaround_forward_declaration.hpp:
#ifndef MYLIB_WORKAROUND_FORWARD_DECLARATION // or #pragma once if supported
#define MYLIB_WORKAROUND_FORWARD_DECLARATION
// amount of error checking is up to you
// eg, are workaround header files mixed with the regular ones
// or included in the wrong order
template<template<int> class A, int n = 0, typename obj_type = typename A<n>::type>
struct Instantiate;
#endif
myLib_workaround_implementation.hpp:
#ifndef MYLIB_WORKAROUND_IMPLEMENTATION // or #pragma once if supported
#define MYLIB_WORKAROUND_IMPLEMENTATION
// amount of error checking is up to you
// eg, are workaround header files mixed with the regular ones
// or included in the wrong order
template<template<int> class A, int n, typename obj_type>
struct Instantiate : public Instantiate<A,n+1>
{
obj_type object;
Instantiate() : object() {}
};
template<template<int> class A, int n>
struct Instantiate<A,n,void>
{
};
#endif
用户文件.cpp:
// Forward declaration of problematic inherited class template
#include "myLib_workaround_forward_declaration.hpp"
// Now compiles!
template<typename O>
struct test
{
template<int n, bool=true> struct array { typedef void type; };
template<int n> struct array2 { typedef typename array<n>::type type; };
template<bool u> struct array<0,u> { typedef int type; };
template<bool u> struct array<1,u> { typedef float type; };
template<bool u> struct array<2,u> { typedef bool type; };
Instantiate<array2> objects;
};
#include "myLib_workaround_implementation.hpp"
int main()
{
test<int> obj;
return 0;
}