我g++在以下代码中从3.3中得到一个奇怪的错误:
#include <bitset>
#include <string>
using namespace std;
template <int N, int M>
bitset<N> slice_bitset(const bitset<M> &original, size_t start) {
string str = original.to_string<char, char_traits<char>, allocator<char> >();
string newstr = str.substr(start, N);
return bitset<N>(newstr);
}
int main() {
bitset<128> test;
bitset<12> result = slice_bitset<12, 128>(test, 0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误如下:
In function `std::bitset slice_bitset(const std::bitset&, unsigned int)': syntax error before `,' token `char_traits' specified as declarator-id two or more data types in declaration of `char_traits' `allocator' specified as declarator-id two or more data types in declaration of `allocator' syntax error before `>' token
它必须是非常愚蠢的东西,但我已经告诉我的橡皮鸭和朋友无济于事.
谢谢,Lazyweb.
Ric*_*den 10
上面的CAdaker选择的答案解决了问题,但没有解释为什么它解决了问题.
在解析函数模板时,查找不会发生在依赖类型中.因此,可以解析以下构造:
template <typename T>
class B;
template <typename T>
void foo (B<T> & b) {
// Use 'b' here, even though 'B' not defined
}
template <typename T>
class B
{
// Define 'B' here.
};
Run Code Online (Sandbox Code Playgroud)
然而,这个"特征"有成本,在这种情况下,'foo'的定义需要提示模板'B'的内容.如果'foo'使用嵌套类型'B',那么typename关键字需要告诉编译器该名称是一个类型:
template <typename T>
void foo (B<T> & b)
{
typename B<T>::X t1; // 'X' is a type - this declares t1
B<T>::Y * t1; // 'Y' is an object - this is multiplication
}
Run Code Online (Sandbox Code Playgroud)
如果没有上面的'typename',编译器将假定它X是一个对象(或函数).
类似地,如果调用成员函数并且调用具有显式模板参数,则编译器需要知道将其<视为模板参数列表的开头而不是小于运算符:
template <typename T>
void foo (B<T> & b)
{
b.template bar<int> (0); // 'bar' is a template, '<' is start of arg list
b.Y < 10; // 'Y' is an object, '<' is less than operator
}
Run Code Online (Sandbox Code Playgroud)
如果没有template,编译器会假定它<是小于运算符,因此在看到时会生成语法错误,int>因为它不是表达式.
即使模板的定义可见,也需要这些提示.原因是显式特化可能会在以后更改实际选择的定义:
template <typename T>
class B
{
template <typename S>
void a();
};
template <typename T>
void foo (B<T> & b)
{
b.a < 10; // 'B<int>::a' is a member object
}
template <>
class B<int>
{
int a;
};
Run Code Online (Sandbox Code Playgroud)
使用其中一个
original.to_string();
Run Code Online (Sandbox Code Playgroud)
或者,如果你真的需要类型说明符,
original.template to_string<char, char_traits<char>, allocator<char> >();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14501 次 |
| 最近记录: |