首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么“使用命名空间”尝试为MSVC实例化名称空间中的模板?

为什么“使用命名空间”尝试为MSVC实例化名称空间中的模板?
EN

Stack Overflow用户
提问于 2018-08-08 10:31:49
回答 2查看 189关注 0票数 1

我有以下代码示例( MSVC 2015编译)

https://godbolt.org/g/kccgtb (用于可执行演示)

代码语言:javascript
复制
#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的指导下,它进行了编译。

请参阅https://godbolt.org/g/kccgtb

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-08 11:20:55

其中一种方法是在模板参数中使用SFINAE:

代码语言:javascript
复制
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)
{ 
}

它比有争议的那个更简洁,也更少雾。

票数 3
EN

Stack Overflow用户

发布于 2018-08-08 10:59:43

我有一个工作学院的工作。它很丑,但似乎很管用

代码语言:javascript
复制
#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(){}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51744461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档