[英]Why does “using namespace” try to instantiate templates in that namespace for MSVC
我有以下代码示例(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 下产生以下错误
例子.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): 错误 C2039: 'type': 不是 'boost::range_iterator' 的成员
with [ T=unsigned __int64 ]
/opt/compiler-explorer/libs/boost_1_67_0\\boost/range/value_type.hpp(26):注意:参见'boost::range_iterator'的声明
with [ 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): 注意:在编译类模板成员函数 'void std::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:语法错误:在标识符“类型”之前缺少“>”
/opt/compiler-explorer/libs/boost_1_67_0\\boost/iterator/iterator_traits.hpp(23): 错误 C2039: 'value_type': 不是 'std::iterator_traits' 的成员
with [ Iterator=int ]
/opt/compiler-explorer/libs/boost_1_67_0\\boost/iterator/iterator_traits.hpp(23): 注意:见'std::iterator_traits'的声明
with [ 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++ 不支持 default-int
编译器返回:2
在 gcc 和 clang 下编译。
一种方法是在模板参数中使用 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)
{
}
它比带有 sfinae 参数的更简洁,更少模糊
我有一个工作同事的工作。 这很丑陋,但似乎有效
#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(){}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.