c++ - 模板化重复类型错误

标签 c++ visual-studio-2010 templates

我在使用 VS2010 时遇到了一个奇怪的问题。以下代码无法编译(虽然是 perfectly accepted by 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 错误?
  • 如果 VS 错误,是否有解决方法?

编辑: - 从杰里的回答来看,这似乎是一个编译器错误......并且它已在 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 提示递归级别,而不是 undefined symbol )。无论如何,这是一个非常有趣的例子。我还在 VS 2005 中看到了相同的行为(使用 Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86)。似乎 Microsoft 仍不接受 VS 2010 的错误报告(根据 Microsoft Connect Visual Studio pagefeedback form 判断)。希望有权访问 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,然后包含您的 header 。否则,您可以创建一个头文件,其中包含前向声明以及所需的其他内容。您很可能希望为受此问题影响的用户创建特殊的解决方法 header 。这肯定会使事情变得非常困惑且容易出错(主要是由于默认模板参数),但它会起作用:

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

关于c++ - 模板化重复类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15620686/

相关文章:

c++ - 如何修复此 C++ 类型列表模板编译错误?

c++ - 如何在最小化窗口后最大化它

c++ - 旧的 Borland C 程序

c# - Visual Studio 扩展 : Editing code files in the current solution?

c++ - 尽管需要,但从未调用基类的重载函数

c++ - 构造函数不更新类成员变量

visual-studio-2010 - Visual Studio 2010 可移植类库不允许我更改目标

c - 指向字符的指针

c++ - 具有专门用于 C++ 的单一方法的模板类

C++ `using` 模板类中类型别名的命令