在 boost::type_traits::条件中使用类型特征的编译错误
Compilation error with Type Traits in boost::type_traits::conditional
我在使用 boost type_traits的某些代码中遇到了问题。这是代码中相当复杂的部分,但我可以隔离导致编译错误的部分:
template<const size_t maxLen>
class MyString {
public:
typedef boost::conditional<(maxLen > 0), char[maxLen+1], std::string> ObjExternal;
};
template <class T>
class APIBase {
public:
typedef T obj_type;
typedef typename T::ObjExternal return_type;
};
template <class T>
int edit(const T& field, const typename T::return_type& value)
{
return 0;
}
int myFunction()
{
APIBase<MyString<10> > b;
char c[11];
return edit(b, c);
}
这将给出以下错误:
test.cpp: 在函数 'int myFunction()' 中:tes.cpp:109:错误:调用"edit(APIBase>&, char [11])"没有匹配函数tes.cpp:100:注意:候选者是:int edit(const T&,const typename T::return_type&) [T = APIBase>]
但是,如果我用代码更改行
char c[11];
由
MyString<10>::ObjExternal c;
它有效。同样,如果我改行
typedef boost::conditional<(maxLen > 0), char[maxLen+1], std::string> ObjExternal;
由
typedef char ObjExternal[maxLen+1];
它也可以工作。我认为这是 boost::condition 的问题,因为它似乎没有被正确评估。我的代码中是否存在问题,或者可以使用替代 boost::condition 的替代方法来具有此功能?
我正在考虑使用第二个选项,但后来我无法使用 maxLen 作为 0。
您需要使用 conditional
提供的成员 typedef type
,而不是条件类型本身。
改变:
typedef boost::conditional<(maxLen > 0),
char[maxLen+1],
std::string> ObjExternal;
自:
typedef typename boost::conditional<(maxLen > 0),
char[maxLen+1],
std::string>::type ObjExternal;
相关文章:
- 根据C++标准的定义实现"is_similar"类型特征
- C++类型特征,以查看是否可以<uint32_t>对类型"K"的任何变量调用"static_cast(k)"
- C ++类型特征:确保子类实现方法
- 根据类型特征更改函数定义?
- 如何使用类型特征将函数的通用引用参数限制为 r 值引用?
- 在C++中创建新的类型特征
- 如何构造一个类型特征,可以判断一个类型的私有方法是否可以在另一个类型的构造函数中调用?
- std::begin-类型特征中未考虑用户定义的重载
- 在类型特征修改后,无法限定类型
- 使用类型特征的部分类专用化
- 为什么不调用预期的函数?我是否对类型特征的理解不正确?
- 了解类型特征的体系结构
- 为什么使用某些类型特征的模板类型会导致模板扣除失败
- 有了C++类型特征,有没有办法避免铸造
- 哪种类型特征表明该类型是可分配的?(元组,对)
- 一种类型特征,标识哪个类提供通过重载解析选择的函数
- 如何使用类型特征正确推断引用的参数
- 如何通过扩展以下类型特征来删除 decltype(&MyClass::funct) 部分?
- 从标准库类型特征继承
- 为什么类型特征不适用于命名空间范围内的类型?