c++模板元编程专门化歧义
C++ Template Metaprogramming Specialization Ambiguity
所以我刚开始用模板元编程,我一直在写一个字符串类。我实现了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> { ... }
如何解决这个歧义?
谢谢。
这里您定义了Substring
与0
, 0
和任何class S
:
template <class S>
struct Substring<0, 0, S> {
typedef Null substr;
};
这里你定义了一个Substring
, I
, 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;
};
那么这将比任何其他的都更专门化。但是,在您的代码中可能存在其他的歧义来源。
相关文章:
- 构造对象的歧义
- 是否可以对零模板参数进行模板专门化
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 尝试根据类中 typedef 的存在来专门化模板函数
- 如何基于模板化类的基类专门化成员函数
- 数组初始值设定项的构造函数歧义
- 消除好友和成员二进制运算符的歧义
- 用' . '代替' :: '会在C++中造成歧义吗?
- 为什么下面带有非常量转换函数的代码没有歧义?
- 如何为指向复杂值的迭代器专门化算法?
- 专门化模板覆盖函数/避免对象切片
- 我能否根据其运算符()的签名专门化可变参数模板参数
- std::bind 是否实现了 std::ref 和 std::cref 来消除函数调用的歧义?
- 如何使用模板化类专门化模板化函数?
- 使用 bool 和 const char 重载的 C++ 函数会在没有警告的情况下产生歧义 (MSVC2012)
- 线程 std::调用未知类型,无法专门化函数错误
- 如何使用is_base_of专门化模板,而不会与主模板产生歧义
- 可变类型的部分模板专门化和扩展到外部类型的可变参数包会导致歧义
- 部分模板专门化歧义
- c++模板元编程专门化歧义