2

我在使用 VS2010 时遇到了一个奇怪的问题。以下代码无法编译(虽然被gcc 完全接受):

// 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 = 0, typename obj_type = typename A<n>::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>
{
};

// Does not compile
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;
};

int main()
{
   test<int> obj;
}

出现以下错误:

C1202: recursive type or function dependency context too complex
see instanciation of class template 'Instantiate<A,n>'
with
[
    A=test<O>::array2,
    n=1
]
(and so on, up to n=497...)

制作test一个非模板(即,只是删除 line template<typename O>)可以解决该错误。然而,尽管在这个简单的例子中是可能的,但在我的代码中是不可能的(这要复杂得多)。

问题是:

  • 哪个是正确的,VS 或 GCC ?或者它是一个VS错误?
  • 如果 VS 错误,是否有解决方法?

编辑: - 从杰里的回答来看,它似乎是一个编译器错误......并且它已在 VS2012 上修复。- 生成的类层次结构应该是:

Instantiate<array2, 3, void> // end of recursion
      ^
      |
Instantiate<array2, 2, bool>
      ^
      |
Instantiate<array2, 1, float>
      ^
      |
Instantiate<array2, 0, int>
4

3 回答 3

1

我可以在 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;
}
于 2013-03-26T17:26:10.697 回答
0

在搞乱rise4fun和jerry的解决方法时,我发现将Instantiate inside的整个定义 test “解决”了问题有关VS2012CTP vs VS2012RTM的更多信息,请参见jerry的回答):

template<typename O>
struct test
{
    template<template<int> class A, int n = 0, typename obj_type = typename A<n>::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>
    {
    };

    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;
};

int main()
{
   test<int> obj;
}

这非常适合我的用例,因为它可以包含在用户已经使用的宏中。

于 2013-03-27T20:37:28.560 回答
0

这不是错误,而是编译器配置。编译时堆栈就像运行时堆栈一样受到限制。VS 中的 IIRC 编译时堆栈的最大深度为 512。可能值得在文档中查找,也许它是可配置的,但不确定。

于 2013-03-25T17:11:20.757 回答