Beware the C++ implicit conversion

 http://blogs.msdn.com/oldnewthing/archive/2006/05/24/605974.aspx and a lot of comments there.

Enjoy!

Beware the C++ implicit conversion

Today's topic was inspired by a question from a customer:

I am working on a stack overflow bug. To reduce the size of the stack frame, I removed as many local variables as I could, but there's still a a lot of stack space that I can't account for. What else lives on the stack aside from local variables, parameters, saved registers, and the return address?

Well, there's also structured exception handling information, but that's typically not too much and therefore wouldn't be the source of "a lot" of mysterious stack usage.

My guess is that the code is generating lots of large C++ temporaries. Consider the following program fragment:

class BigBuffer
{
public:
 BigBuffer(int initialValue)
   { memset(buffer, initialValue, sizeof(buffer)); }
private:
 char buffer[65536];
};

extern void Foo(const BigBuffer& o);

void oops()
{
 Foo(3);
}

"How does this code even compile? The function Foo wants a BigBuffer, not an integer!" Yet compile it does.

That's because the compiler is using the BigBuffer constructor as a converter. In other words, the compiler inserted the following temporary variable:

void oops()
{
 BigBuffer temp(3);
 Foo(temp);
}

It did this because a constructor that takes exactly one argument serves two purposes: It can be used as a traditional constructor (as we saw with BigBuffer temp(3)) or it can be used to provide an implicit conversion from the argument type to the constructed type. In this case, the BigBuffer(int) constructor is being used as a conversion from int to BigBuffer.

To prevent this from happening, use the explicit keyword:

class BigBuffer
{
public:
 explicit BigBuffer(int initialValue)
   { memset(buffer, initialValue, sizeof(buffer)); }
private:
 char buffer[65536];
};

With this change, the call to Foo(3) raises a compiler error:

sample.cpp: error C2664: 'Foo' : cannot convert parameter 1 from
     'int' to 'const BigBuffer &'
     Reason: cannot convert from 'int' to 'const BigBuffer'
     Constructor for class 'BigBuffer' is declared 'explicit'
<think>好的,我需要帮助用户了解如何通过C++调用Concord TSP Solver和LHK,以及相关的许可证和执行要求。首先,我需要回顾一下这两个工具的基本情况,以及它们的调用方式。 Concord TSP Solver是由MIT开发的旅行商问题求解器,基于分支切割法,适用于大规模问题。它主要作为独立程序运行,用户可能需要通过系统调用或文件交互来使用。而LKH(由Keld Helsgaun开发)是另一个高效的TSP求解器,使用迭代局部搜索,支持多种扩展问题。用户提到的wrapper可能是指C++封装接口,但这两个工具本身是否提供C++ API呢?我需要确认这一点。 首先,查阅Concord的文档,发现它主要提供命令行工具,可能需要通过生成TSP实例文件,调用命令行,然后解析输出结果。同样,LKH也是以可执行文件形式分发,通常通过参数和文件进行配置。因此,在C++中调用它们可能需要使用系统命令执行,或者利用进程间通信的库,比如popen或Boost.Process。 关于许可证,Concord使用MIT许可证,允许自由使用和修改,但需保留版权声明。而LKH虽然学术用途免费,但商用可能需要授权,用户需要仔细阅读其许可证文件。这一点必须准确,避免用户侵权。 接下来,用户需要将可执行文件添加到系统路径中,或者在代码中指定完整路径。例如,在Linux下,可以将concord和LKH的可执行文件放在/usr/local/bin/,或者在代码中使用绝对路径调用。 编写C++封装器的话,可能需要创建一个类或函数,生成问题文件,调用外部程序,读取输出,并处理结果。例如,使用system()函数或者更安全的进程处理库来执行命令,然后读取生成的结果文件,解析数据。 测试代码部分,用户可能需要包含生成TSPLIB格式的文件,调用concord或LKH,并读取结果。例如,用C++的文件操作生成问题文件,执行求解器,然后读取解决方案文件的内容。 注意事项方面,路径问题、错误处理、跨平台兼容性都需要考虑。比如在Windows和Linux下路径分隔符不同,需要处理。此外,错误处理要检查外部程序是否执行成功,是否有输出文件生成。 最后,生成相关问题需要考虑用户可能的后续疑问,比如性能优化、其他TSP求解器比较、许可证细节等。 总结步骤:首先介绍两个求解器的基本情况,说明调用方式(命令行),编写C++封装的步骤(生成输入、调用可执行、解析输出),许可证要求,路径配置,示例代码,注意事项,相关问题。</think>### 通过C++调用Concord TSP Solver和LKH的步骤 #### 1. **工具特性与调用方式** - **Concord TSP Solver** 由MIT开发,基于分支切割法,适合大规模TSP问题。**不提供原生C++ API**,需通过生成TSPLIB格式输入文件(如`.tsp`),调用命令行程序,并解析输出文件(如`.sol`)[^1]。 **示例调用命令**: ```bash concord [输入文件] [输出文件] ``` - **LKH (Helsgaun's TSP Solver)** 使用迭代局部搜索算法,支持TSP及其变体。**同样依赖命令行调用**,需配置参数文件(如`参数.txt`)指定输入输出路径[^2]。 **示例调用命令**: ```bash LKH 参数.txt ``` #### 2. **C++封装实现步骤** - **输入文件生成** 使用C++文件操作生成符合TSPLIB标准的输入文件: ```cpp #include <fstream> void generate_tsp_file(const std::string& filename, const std::vector<Point>& points) { std::ofstream file(filename); file << "NAME: example\nTYPE: TSP\nDIMENSION: " << points.size() << "\nEDGE_WEIGHT_TYPE: EUC_2D\n"; file << "NODE_COORD_SECTION\n"; for (size_t i = 0; i < points.size(); ++i) file << i+1 << " " << points[i].x << " " << points[i].y << "\n"; file << "EOF\n"; } ``` - **调用外部可执行文件** 使用`system()`或进程库执行命令: ```cpp #include <cstdlib> void run_solver(const std::string& command) { int result = system(command.c_str()); if (result != 0) { std::cerr << "Solver execution failed!" << std::endl; } } // 调用示例 run_solver("concord input.tsp output.sol"); ``` - **输出解析** 读取并解析求解器生成的解决方案文件: ```cpp std::vector<int> parse_solution(const std::string& filename) { std::ifstream file(filename); std::vector<int> tour; std::string line; bool in_section = false; while (std::getline(file, line)) { if (line.find("TOUR_SECTION") != std::string::npos) { in_section = true; continue; } if (in_section) { if (line == "-1") break; tour.push_back(std::stoi(line) - 1); // 转换为0-based索引 } } return tour; } ``` #### 3. **许可证与执行要求** - **Concord** - **许可证**: MIT许可证,允许商业使用,需保留版权声明。 - **依赖项**: 需安装CBC(COIN-OR Branch and Cut)和动态链接库(如`libCbc.so`)。 - **LKH** - **许可证**: 学术免费,**商用需授权**(需联系Keld Helsgaun)。 - **路径要求**: 必须将`LKH`可执行文件置于系统路径或指定绝对路径。 #### 4. **环境配置示例** - **Linux系统** ```bash # 将可执行文件复制到系统路径 sudo cp concord /usr/local/bin/ sudo cp LKH /usr/local/bin/ # 确保动态库可访问 sudo ldconfig ``` - **Windows系统** 将`concord.exe`和`LKH.exe`所在目录添加到`PATH`环境变量。 #### 5. **完整代码示例** ```cpp #include <iostream> #include <vector> #include <fstream> #include <cstdlib> struct Point { double x, y; }; int main() { // 1. 生成TSP输入文件 std::vector<Point> cities = {{0,0}, {1,2}, {3,1}, {2,3}}; generate_tsp_file("input.tsp", cities); // 2. 调用Concord求解器 run_solver("concord input.tsp output.sol"); // 3. 解析结果 auto tour = parse_solution("output.sol"); std::cout << "Optimal tour: "; for (int city : tour) std::cout << city << " "; return 0; } ``` #### 6. **注意事项** - **错误处理**: 检查文件是否存在、命令执行返回值。 - **跨平台兼容性**: 使用`boost::process`替代`system()`以增强控制。 - **性能优化**: 减少文件I/O开销,例如使用内存映射或临时文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值