模板化反复出现的类型错误

Templated recurrent type error

本文关键字:类型 错误      更新时间:2023-10-16

我在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为非模板(即,仅删除行template<typename O>)解决了这个错误。然而,尽管在这个简单的例子中是可能的,但在我的代码中是不可能的(这要复杂得多)。

问题是:

  • 哪个是正确的,VS还是GCC ?或者这是一个VS bug ?
  • 如果VS是错误的,是否有解决方案?

编辑:-从jerry的回答来看,这似乎是一个编译器错误…它是固定在VS2012。生成的类层次结构应该是:

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

我可以在VS 2010(与Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86)中重现这一点,我相信这是一个错误。说实话,我很难理解这里涉及的作用域问题,但这似乎与手头的问题无关(因为基本模板和专门化嵌套在一起,VS抱怨的是递归级别,而不是未定义的符号)。无论如何,这是一个非常有趣的例子。我在vs2005(使用Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86)中也看到了相同的行为。微软似乎不再接受VS 2010的bug报告(从Microsoft Connect Visual Studio页面和反馈表单判断)。希望有访问VS 2012的人能够测试你的原始代码并提交错误报告,如果问题尚未解决(我认为很有可能,我没有看到任何迹象表明它已经引起了微软的注意)。

要解决这个问题,请向前声明导致递归的类模板:

// 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,然后包括您的头。否则,您可以创建一个头文件,其中包含forward声明以及所需的其他内容。您很可能希望为受此问题影响的用户创建特殊的变通头。这当然会使事情变得非常混乱和容易出错(主要是由于默认的模板参数),但它会工作:

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

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

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

在与rise4fun和jerry的解决方案混淆时,我发现将Instantiate 的整个定义放在 test中"解决"了问题(请参阅jerry的回答,了解有关VS2012CTP与VS2012RTM的更多信息):

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

这是完美的我的用例,因为它可以包含在一个宏已经被用户使用。