C++ 模板元编程特化歧义

标签 c++ templates metaprogramming template-specialization template-meta-programming

所以我刚开始使用模板元编程,并且一直在编写一个字符串类。我实现了 ToString、Concat、CharAt 和 Length,没有太多与模板相关的问题。我试图按如下方式实现 Substring:

struct Null;

// String class definition
template <char C, class S>
struct String {
  static const char chr = C;
  typedef S tail;
};

// Substring
// Gets the substring of length L starting at index I from string S.
template <int I, int L, class S>
struct Substring;

template <class S>
struct Substring<0, 0, S> {
  typedef Null substr;
};

// Will also cover I < 0 case
template <int I, int L>
struct Substring<I, L, Null> {
  typedef Null substr;
};

template <int L, char C, class S>
struct Substring<0, L, String<C, S> > {
  typedef String<C, typename Substring<0, L-1, S>::substr> substr;
};

template <int I, int L, char C, class S>
struct Substring<I, L, String<C, S> > {
  typedef typename Substring<I-1, L, S>::substr substr;
};

int main() {
  // This all works...
  typedef String<'H', String<'e', String<'l', String<'l',
            String<'o', Null> > > > > hello;
  typedef String<',', String<' ', Null> > comma;
  typedef String<'w', String<'o', String<'r', String<'l', String<'d',
            String<'!', Null> > > > > > world;
  typedef Concat<hello, Concat<comma, world>::newstr>::newstr hello_world;
  // ...up to here.
  typedef Substring<3, 5, hello_world>::substr mystr;
  return 0;
}

编译时出现歧义错误:

template.cpp:161: error: ambiguous class template instantiation for ‘struct
    Substring<0, 0, String<'o', String<'r', String<'l', String<'d', String<'!',
    Null> > > > > >’
template.cpp:149: error: candidates are: struct Substring<0, 0, S>
template.cpp:160: error:                 struct Substring<0, L, String<C, S> >
template.cpp:165: error:                 struct Substring<I, L, String<C, S> >
template.cpp:161: error: invalid use of incomplete type ‘struct Substring<0, 0, 
    String<'o', String<'r', String<'l', String<'d', String<'!', Null> > > > > >’
template.cpp:146: error: declaration of ‘struct Substring<0, 0, String<'o',
    String<'r', String<'l', String<'d', String<'!', Null> > > > > >’
template.cpp: In function ‘int main()’:
template.cpp:197: error: template argument 1 is invalid

我有点困惑。我认为模板特化的全部意义在于做这样的事情。为什么这不只是以下内容的扩展:

template <int N>
struct Foo { ... }

template <>
struct Foo<0> { ... }

如何解决这种歧义?

谢谢。

最佳答案

这里你定义了一个Substring0 , 0和任何 class S :

template <class S>
struct Substring<0, 0, S> {
  typedef Null substr;
};

这里你定义了一个SubstringI , L和一个 String< C, S > :

template <int I, int L, char C, class S>
struct Substring<I, L, String<C, S> > {
  typedef typename Substring<I-1, L, S>::substr substr;
};

没有人比另一个更好,因为一个人更适合 I, L但对 String< C, S > 的匹配更差.如果您要将第一个案例声明为:

template <char C, class S>
struct Substring<0, 0, String< C, S > > {
  typedef Null substr;
};

那么这将比其他任何一个都更专业。但是,您的代码中可能存在其他歧义来源。

关于C++ 模板元编程特化歧义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7923454/

相关文章:

c++ - 理解 argList 构造函数的问题

c++ - (void) sizeof (0[array]) 是什么意思?

c++ - 从这里实例化错误

arguments - Elixir,从列表或元组中解包参数

ruby - 在 Ruby 中为 new 使用正确数量的参数

c++ - 在面向对象编程中,哪些函数调用没有在编译时解析?

c++ - 调用 constexpr 函数的给定重载时触发编译时错误

c++ - 初始化列表中的抽象类init

c++ - 在什么情况下 "name"必须以 "typename"为前缀?

ruby-on-rails - Rails ActiveRecord 存储中的动态属性