gpt4 book ai didi

c++ - 使用 C++11 选项编译 C++98 代码时,由于 Boost header 导致的不明确重载

转载 作者:行者123 更新时间:2023-11-27 23:54:12 24 4
gpt4 key购买 nike

摘要:

我的 C++98 代码使用一些 boost 库和一些在自己的命名空间中定义的自己的函数(存在于 C++11 中)。它与 gcc 4.8.x 编译得很好。当我尝试使用选项 -std=c++11 编译它时,我自己得到了一个 call to overloaded xxx is ambiguous 错误在我自己的命名空间中运行。此函数存在于 C++11 的命名空间 std 中。看起来像 boost 在 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 时,它看起来真的很像 boost 调用 using namespace std; 的 header 。

为什么?在我看来,在 header 中调用 using namespace ...; 是一种非常糟糕的做法...这是错误吗?

有没有对我的代码没有干扰的简单解决方案?

注意:我必须使用选项 -std=c++11 进行编译才能使用 Boost.Process (来自 Boost 1.64)在我的代码的特定模块中,针对特定平台,具有特定的编译标志。其余代码必须在旧 gcc (4.4.x) 下编译。

最佳答案

Argument-dependent_name_lookup ( ADL) 的实际应用:

x.begin()返回类型是 std::vector<double>::iterator .如果它属于 std命名空间,然后

is_sorted(x.begin(), x.end())

在命名空间 std 中查找也是。

Boost 可能会在其 header 中有条件地添加一些包含 ( <algorithm> ) 以支持 c++11 功能。

这发生在没有任何 using namespace std; 的情况下

关于c++ - 使用 C++11 选项编译 C++98 代码时,由于 Boost header 导致的不明确重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43869229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com