C++ 模板特化和 xcode

标签 c++ xcode templates c++11 c++14

Xcode 出现以下错误:

Template argument for template template parameter must be a class template or type alias template

然而,同样的代码在 Visual Studio 中运行良好。

template< typename T_NODE, template< typename > class T_ALLOCATOR >
class qt_fixed_pool_general {};

template< template< typename, int > class T_ALLOCATOR, int T_COUNT >
struct static_to_dynamic
{ template< typename T_NODE > using t_allocator = T_ALLOCATOR< T_NODE,T_COUNT >; };

template< typename T_NODE, int T_COUNT >
struct safe_array {};

template< class T_NODE, int T_COUNT >
class qt_fixed_pool_static : public qt_fixed_pool_general< 
      T_NODE, 
      static_to_dynamic< safe_array, T_COUNT >::t_allocator >
{};

任何想法可能会发生什么? 我正在使用 Xcode 7.2

最佳答案

问题是当解析(而不是实例化)模板时,编译器无法知道 t_allocator 将是一个模板而不是静态数据成员。你必须告诉它:

template< class T_NODE, int T_COUNT >
class qt_fixed_pool_static : public qt_fixed_pool_general< 
      T_NODE, 
      static_to_dynamic< safe_array, T_COUNT >::template t_allocator >
{};                                           //^^^^^^^^

[Live example]

注意 ::t_allocator 之间添加的 template 关键字。

这在 Visual Studio 中起作用的原因是 VS 在其模板解析中不符合标准(而且从来没有):它没有正确地进行两阶段查找,而是将所有名称解析推迟到实例化时间,所以它不会因为这样的错误而跳闸。

关于C++ 模板特化和 xcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34544989/

相关文章:

c++ - 将映射 <string int> 写入文件 - 与 ‘operator<<’ 不匹配

c++ - char 的 unicode 被误用于 int

xcode - 我可以将USDZ转换为诸如STL的实体网格吗

c++ - 使用 numeric_limits 作为默认参数值

wpf - 从后面的代码访问自定义控件的 WPF 模板

c++ - 我正在尝试使用这种方法来检测移动物体。有人可以为此建议我吗?

c++ - 标准是否保证 uint8_t、int8_t 和 char 都是唯一类型?

ios - Swift 2.2 和 2.3 互操作性

swift - ObervableObject 被多次初始化,并且没有刷新我的 View

c++ - 如何使用带有模板参数和参数包的 enable if?