【发布时间】:2010-12-08 16:46:14
【问题描述】:
其实我在用 intel 编译器编译一些库时遇到了问题。
这个库已经用 g++ 正确编译了。
问题是由模板引起的。
我想了解的是声明
**typename** 作为函数体内部的非模板函数参数和变量声明
示例:
void func(typename sometype){..
...
typename some_other_type;
..
}
编译这种代码会产生以下错误(intel),(gcc没有声明): 我有以下错误
../../../libs/log/src/attribute_set.cpp(415): error: no operator "!=" matches these operands
operand types are: boost::log_st::basic_attribute_set<wchar_t>::iter<'\000'> != boost::log_st::basic_attribute_set<wchar_t>::iter<'\000'>
while (begin != end)
^
detected during instantiation of "void boost::log_st::basic_attribute_set<CharT>::erase(boost::log_st::basic_attribute_set<CharT>::iter<'\000'>, boost::log_st::basic_attribute_set<CharT>::iter<'\000'>) [with CharT=wchar_t]" at line 438
../../../boost/log/attributes/attribute_set.hpp(115): error: no operator "!=" matches these operands
operand types are: boost::log_st::basic_attribute_set<wchar_t>::iter<'\000'> != boost::log_st::basic_attribute_set<wchar_t>::iter<'\000'>
if (it != m_pContainer->end())
我想了解的是函数体、参数声明中类型名的用法。
例如:
template< typename CharT >
struct basic_attribute_values_view< CharT >::implementation
{
public:
..
..
void adopt_nodes( **typename attribu**te_set_type::const_iterator& it, **typename attribut**e_set_type::const_iterator end)
{
for (; it != end; ++it)
push_back(it->first, it->second.get());
}
我在不同的文件中:
template< typename CharT >
class basic_attribute_set
{
friend class basic_attribute_values_view< CharT >;
//! Self type
typedef basic_attribute_set< CharT > this_type;
public:
//! Character type
typedef CharT char_type;
//! String type
typedef std::basic_string< char_type > string_type;
//! Key type
typedef basic_slim_string< char_type > key_type;
//! Mapped attribute type
typedef shared_ptr< attribute > mapped_type;
//! Value type
typedef std::pair< const key_type, mapped_type > value_type;
//! Allocator type
typedef std::allocator< value_type > allocator_type;
//! Reference type
**typedef typename allocator_type::reference reference;**
【问题讨论】:
-
这会更容易...如果我们有生成编译器错误的代码(指示有罪的行)。请注意,使用 '**' 表示语法在代码块中不起作用。
-
我的示例中的所有迭代器 != 操作都会产生类似的错误,因此在我的第一个代码示例中将是 (it != end;) 行。
标签: c++ boost generic-programming