相关疑难解决方法(0)

为什么使用 __LINE__ 的这段代码在发布模式下在 MSVC 下编译,而不是在调试模式下?

考虑这个程序:

#include <iostream>

template<bool Debug = false, int Line = __LINE__>
constexpr int adds(const int& a, const int& b) { 
    if (Debug)
        std::cout << __FUNCTION__ << " called on line " << Line << '\n';
    return (a + b);
}

int main() {
    std::cout << adds(3, 7) << '\n';
    std::cout << adds<true, __LINE__> (5, 9) << '\n';
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试在DebugVisual Studio 2017 模式下编译和构建它时,会生成这些编译器错误:

1>------ Build started: Project: Simulator, Configuration: Debug x64 ------
1>main2.cpp
1>c:\***\main2.cpp(12): error C2672: 'adds': …
Run Code Online (Sandbox Code Playgroud)

c++ compiler-errors visual-c++ compiler-bug visual-studio-2017

19
推荐指数
1
解决办法
1356
查看次数