以下函数derefItemX()
在GCC 4.8-5.3上编译正常,但在CLang 3.8上失败:
//! Accessory Operations - template argument depended wrappers
template<bool SIMPLE> // For Nodes / non-scoped storage
struct Operations {
//! \brief Defererence wrapped or direct iterator
//!
//! \param iel IItemXT& - iterator to be dereferenced
//! \return ItemT& - resulting reference
template<typename IItemXT>
constexpr static auto& derefItemX(IItemXT& iel)
{
static_assert(is_base_of<std::forward_iterator_tag, typename IItemXT::iterator_category>::value
, "derefItemX(), IItemXT must be a forward iterator type");
return **iel; // Dereference an iterator of pointer to the value
}
};
//! Specialization for non-scoped storage (direct pointers)
template<>
template<typename IItemXT>
constexpr auto& Operations<true>::derefItemX(IItemXT& iel)
{
static_assert(is_base_of<std::forward_iterator_tag, typename IItemXT::iterator_category>::value
, "derefItemX(), IItemXT must be a forward iterator type");
return *iel; // Dereference an iterator of value to the value
}
...
// Usage:
auto& el = Operations<!is_pointer<typename IItemXT::value_type>
::value>::derefItemX(ic);
derefItemX()
取消引用值或指向该值的指针到原始值的迭代器。 CLang显示以下错误消息:
这说不多:
include/hierarchy.hpp:168:35: error: out-of-line definition of 'derefItemX' does not match any declaration in 'XXX::Operations<true>'
constexpr auto& Operations<true>::derefItemX(IItemXT& iel)
^~~~~~~~~~
任何人都可以解释一下:
derefItemX()
?非常感谢!
注意:
当指定返回类型时,C ++ 11也存在同样的问题,但在模板声明和专门化中不同。
看起来CLang需要匹配返回类型(模板类声明和特化中的模板函数),这些类型不是根据标准的函数签名的一部分。 &#34;交叉编译器&#34; @ max66指定的解决方案是具有模板类和所需特化的空声明。
答案 0 :(得分:4)
我不知道如何以一般方式解决问题;但这个问题(如果我没有错)可以解决整个班级的问题;
之类的东西#include <iterator>
#include <type_traits>
using namespace std;
template <bool>
struct Operations;
template<>
struct Operations<false> {
template<typename IItemXT>
constexpr static auto& derefItemX(IItemXT& iel)
{
static_assert(is_base_of<std::forward_iterator_tag, typename IItemXT::iterator_category>::value
, "derefItemX(), IItemXT must be a forward iterator type");
return **iel; // Dereference an iterator of pointer to the value
}
};
template<>
struct Operations<true> {
template<typename IItemXT>
constexpr auto& derefItemX(IItemXT& iel)
{
static_assert(is_base_of<std::forward_iterator_tag, typename IItemXT::iterator_category>::value
, "derefItemX(), IItemXT must be a forward iterator type");
return *iel; // Dereference an iterator of value to the value
}
};
关于问题&#34;为什么clang无法编译&#34; ...我感到困惑,我不知道g ++和clang ++之间的对话
p.s:抱歉我的英语不好
答案 1 :(得分:0)
我最近使用std::enable_if<>
重新编写了该模板,它在所有编译器上运行良好,并且非常优雅地解决了问题(没有显式参数):
//! \brief Defererence wrapped or direct iterator
//!
//! \param iel IItemXT - iterator to be dereferenced
//! \return ItemT& - resulting reference
template <typename IItemXT, enable_if_t<is_pointer<typename iterator_traits<IItemXT>::value_type>::value>* = nullptr>
constexpr auto derefIterX(IItemXT iel) -> remove_pointer_t<typename iterator_traits<IItemXT>::value_type>&
{
static_assert(is_iterator<IItemXT>(), "derefIterX(), IItemXT must be a forward iterator type");
return **iel;
}
template <typename IItemXT, enable_if_t<!is_pointer<typename iterator_traits<IItemXT>::value_type>::value, bool>* = nullptr>
constexpr auto derefIterX(IItemXT iel) -> typename iterator_traits<IItemXT>::reference
{
static_assert(is_iterator<IItemXT>(), "derefIterX(), IItemXT must be a forward iterator type");
return *iel;
}