我有以下代码示例( MSVC 2015编译)
https://godbolt.org/g/kccgtb (用于可执行演示)
#include "boost/range/value_type.hpp"
namespace foo {
template <typename Range>
typename boost::range_value<Range>::type
operator|(Range const& r, int holder)
{
}
}
using namespace foo;
int main(){
}在msvc下生成以下错误
example.cpp /opt/compiler-explorer/windows/19.00.24210/include/xlocale(341):警告C4530:使用C++异常处理程序,但未启用展开语义。指定/EHsc /opt/compiler-explorer/libs/boost_1_67_0\boost/range/value_type.hpp(26):error C2039:'type':不是'boost::range_iterator‘的成员 与T=unsigned __int64 /opt/compiler-explorer/libs/boost_1_67_0\boost/range/value_type.hpp(26):注:见“boost::range_iterator”声明 与T=unsigned __int64 /opt/compiler-explorer/windows/19.00.24210/include/xstring(1659):注意:参见对正在编译的类模板实例化'boost::range_value‘的引用 /opt/compiler-explorer/windows/19.00.24210/include/xstring(1658):注意:在编译类模板成员函数‘voidstd::basic_string,std::allocator>::shrink_to_fit(void)’时 /opt/compiler-explorer/windows/19.00.24210/include/system_error(661):注意:参见正在编译的函数模板实例化'void std::basic_string,std::allocator>::shrink_to_fit(void)‘的引用。 /opt/compiler-explorer/windows/19.00.24210/include/stdexcept(21):注意:参见对正在编译的类模板实例化'std::basic_string,std::allocator>‘的引用 /opt/compiler-explorer/libs/boost_1_67_0\boost/range/value_type.hpp(26):错误C2146:语法错误:缺少“在标识符‘类型之前”的C2146 '>’> /opt/compiler-explorer/libs/boost_1_67_0\boost/iterator/iterator_traits.hpp(23):error C2039:'value_type':不是'std::iterator_traits‘的成员 用Iterator=int /opt/compiler-explorer/libs/boost_1_67_0\boost/iterator/iterator_traits.hpp(23):注:见“std::iterator_traits”声明 用Iterator=int /opt/compiler-explorer/libs/boost_1_67_0\boost/range/value_type.hpp(27):注意:参见对正在编译的类模板实例化'boost::iterators::iterator_value‘的引用 /opt/compiler-explorer/libs/boost_1_67_0\boost/iterator/iterator_traits.hpp(23):错误C3646:'type':未知覆盖说明符 /opt/compiler-explorer/libs/boost_1_67_0\boost/iterator/iterator_traits.hpp(23):错误C4430:缺少类型说明符- int假设。注意: C++不支持默认-int。 返回的编译器:2
在gcc和clang的指导下,它进行了编译。
发布于 2018-08-08 11:20:55
其中一种方法是在模板参数中使用SFINAE:
template <class Range, class = std::enable_if_t<!std::is_fundamental_v<Range>>>
typename boost::range_value<Range>::type
operator|(
const Range &r
, int holder)
{
}它比有争议的那个更简洁,也更少雾。
发布于 2018-08-08 10:59:43
我有一个工作学院的工作。它很丑,但似乎很管用
#include "boost/range/value_type.hpp"
namespace foo {
template <typename Range>
typename boost::range_value<Range>::type
operator|(
typename std::enable_if<!std::is_fundamental<Range>::value, Range>::type
const& r
, int holder)
{
}
}
using namespace foo;
int main(){}https://stackoverflow.com/questions/51744461
复制相似问题