使用 C++11 选项编译 C++98 代码时,由于 Boost headers 导致的不明确重载
Ambiguous overloading due to Boost headers when compiling C++98 code with C++11 option
摘要:
我的 C++98 代码使用一些 boost 库和一些在自己的命名空间中定义的自己的函数(存在于 C++11 中)。它与 gcc 4.8.x 编译得很好。当我尝试使用选项 -std=c++11
编译它时,我在我自己的命名空间中的自己的函数上遇到 call to overloaded xxx is ambiguous
错误。此函数存在于 C++11 的命名空间 std
中。看起来像 header...
中的 boost 调用 using namespace std;
详情:
考虑以下简单代码:
#include <vector>
#include <iostream>
#include <boost/math/special_functions/erf.hpp>
namespace caduchon
{
template <typename Iterator>
bool is_sorted(Iterator begin, Iterator end)
{
if(begin == end) return true;
for(Iterator it = begin, prev = it++; it != end; prev = it++)
if(*it < *prev) return false;
return true;
}
}
using namespace caduchon;
int main()
{
std::vector<double> x(3);
x[0] = 10.0; x[1] = 13.9; x[2] = 21.3;
std::cout << "Is x sorted ? " << is_sorted(x.begin(), x.end()) << std::endl;
// Imagine here a call to boost::math::erf(double)
return 0;
}
使用 gcc 4.8.5 使用以下命令可以很好地编译它:g++ test.cpp -o test.exe -I /softs/boost/1.63.0/64/gcc/4.8.5/include
如果我在编译命令中添加选项-std=c++11
,我会出错:
g++ test.cpp -o test.exe -I /softs/boost/1.63.0/64/gcc/4.8.5/include -std=c++11
test.cpp: In function ‘int main()’:
test.cpp:28:61: error: call of overloaded ‘is_sorted(std::vector<double>::iterator, std::vector<double>::iterator)’ is ambiguous
std::cout << "is sorted ? " << is_sorted(x.begin(), x.end()) << std::endl;
^
test.cpp:28:61: note: candidates are:
test.cpp:9:7: note: bool caduchon::is_sorted(Iterator, Iterator) [with Iterator = __gnu_cxx::__normal_iterator<double*, std::vector<double> >]
bool is_sorted(Iterator begin, Iterator end)
^
In file included from /usr/include/c++/4.8/algorithm:62:0,
from /softs/boost/1.63.0/64/gcc/4.8.5/include/boost/math/tools/config.hpp:18,
from /softs/boost/1.63.0/64/gcc/4.8.5/include/boost/math/tools/promotion.hpp:26,
from /softs/boost/1.63.0/64/gcc/4.8.5/include/boost/math/special_functions/detail/round_fwd.hpp:12,
from /softs/boost/1.63.0/64/gcc/4.8.5/include/boost/math/special_functions/math_fwd.hpp:26,
from /softs/boost/1.63.0/64/gcc/4.8.5/include/boost/math/special_functions/erf.hpp:13,
from test.cpp:3:
/usr/include/c++/4.8/bits/stl_algo.h:3952:5: note: bool std::is_sorted(_FIter, _FIter) [with _FIter = __gnu_cxx::__normal_iterator<double*, std::vector<double> >]
is_sorted(_ForwardIterator __first, _ForwardIterator __last)
如果删除 boost::math::erf
函数的包含项,我不会出现错误。
如果我将 is_sorted
替换为 caduchon::is_sorted
则不会出现错误(但我不想影响我的所有代码)。
当定义了选项 -std=c++11
时,看起来确实像 header 的提升调用 using namespace std;
。
为什么?在我看来,在 header 中调用 using namespace ...;
是一个非常糟糕的做法...这是一个错误吗?
有没有对我的代码没有干扰的简单解决方案?
注意: 我必须使用选项 -std=c++11
进行编译才能在我的代码的特定模块,针对特定平台,具有特定的编译标志。其余代码必须在旧 gcc (4.4.x).
下编译
Argument-dependent_name_lookup (ADL) 在行动中:
x.begin()
return 类型是 std::vector<double>::iterator
。
如果它属于 std
命名空间,则
is_sorted(x.begin(), x.end())
也在命名空间 std
中查找。
Boost 可能会在其 header.
中有条件地添加一些包含 (<algorithm>
) 以支持 c++11 功能
没有任何 using namespace std;
摘要:
我的 C++98 代码使用一些 boost 库和一些在自己的命名空间中定义的自己的函数(存在于 C++11 中)。它与 gcc 4.8.x 编译得很好。当我尝试使用选项 -std=c++11
编译它时,我在我自己的命名空间中的自己的函数上遇到 call to overloaded xxx is ambiguous
错误。此函数存在于 C++11 的命名空间 std
中。看起来像 header...
using namespace std;
详情:
考虑以下简单代码:
#include <vector>
#include <iostream>
#include <boost/math/special_functions/erf.hpp>
namespace caduchon
{
template <typename Iterator>
bool is_sorted(Iterator begin, Iterator end)
{
if(begin == end) return true;
for(Iterator it = begin, prev = it++; it != end; prev = it++)
if(*it < *prev) return false;
return true;
}
}
using namespace caduchon;
int main()
{
std::vector<double> x(3);
x[0] = 10.0; x[1] = 13.9; x[2] = 21.3;
std::cout << "Is x sorted ? " << is_sorted(x.begin(), x.end()) << std::endl;
// Imagine here a call to boost::math::erf(double)
return 0;
}
使用 gcc 4.8.5 使用以下命令可以很好地编译它:g++ test.cpp -o test.exe -I /softs/boost/1.63.0/64/gcc/4.8.5/include
如果我在编译命令中添加选项-std=c++11
,我会出错:
g++ test.cpp -o test.exe -I /softs/boost/1.63.0/64/gcc/4.8.5/include -std=c++11
test.cpp: In function ‘int main()’:
test.cpp:28:61: error: call of overloaded ‘is_sorted(std::vector<double>::iterator, std::vector<double>::iterator)’ is ambiguous
std::cout << "is sorted ? " << is_sorted(x.begin(), x.end()) << std::endl;
^
test.cpp:28:61: note: candidates are:
test.cpp:9:7: note: bool caduchon::is_sorted(Iterator, Iterator) [with Iterator = __gnu_cxx::__normal_iterator<double*, std::vector<double> >]
bool is_sorted(Iterator begin, Iterator end)
^
In file included from /usr/include/c++/4.8/algorithm:62:0,
from /softs/boost/1.63.0/64/gcc/4.8.5/include/boost/math/tools/config.hpp:18,
from /softs/boost/1.63.0/64/gcc/4.8.5/include/boost/math/tools/promotion.hpp:26,
from /softs/boost/1.63.0/64/gcc/4.8.5/include/boost/math/special_functions/detail/round_fwd.hpp:12,
from /softs/boost/1.63.0/64/gcc/4.8.5/include/boost/math/special_functions/math_fwd.hpp:26,
from /softs/boost/1.63.0/64/gcc/4.8.5/include/boost/math/special_functions/erf.hpp:13,
from test.cpp:3:
/usr/include/c++/4.8/bits/stl_algo.h:3952:5: note: bool std::is_sorted(_FIter, _FIter) [with _FIter = __gnu_cxx::__normal_iterator<double*, std::vector<double> >]
is_sorted(_ForwardIterator __first, _ForwardIterator __last)
如果删除 boost::math::erf
函数的包含项,我不会出现错误。
如果我将 is_sorted
替换为 caduchon::is_sorted
则不会出现错误(但我不想影响我的所有代码)。
当定义了选项 -std=c++11
时,看起来确实像 header 的提升调用 using namespace std;
。
为什么?在我看来,在 header 中调用 using namespace ...;
是一个非常糟糕的做法...这是一个错误吗?
有没有对我的代码没有干扰的简单解决方案?
注意: 我必须使用选项 -std=c++11
进行编译才能在我的代码的特定模块,针对特定平台,具有特定的编译标志。其余代码必须在旧 gcc (4.4.x).
Argument-dependent_name_lookup (ADL) 在行动中:
x.begin()
return 类型是 std::vector<double>::iterator
。
如果它属于 std
命名空间,则
is_sorted(x.begin(), x.end())
也在命名空间 std
中查找。
Boost 可能会在其 header.
中有条件地添加一些包含 (<algorithm>
) 以支持 c++11 功能
没有任何 using namespace std;