如何在 Visual Studio 中查看 static_assert 的“失败源”?

Max*_*xim 1 c++ static-assert visual-studio-2015

在 Visual Studio 中尝试编译时,我得到一个 static_assert,但是当我单击该错误时,它需要断言本身而不是有问题的行。

如何找出哪一行触发了static_assert?

Pau*_*ers 5

我知道你的意思,因为我也使用 Visual Studio,但我认为你没有点击正确的东西。

例如,如果您在 IDE 中编译它:

#include <type_traits>

template <class T> void f1 () { static_assert (std::is_same <T, int>::value, "oh no it isn't"); }
template <class T> void f2 () { f1 <T> (); }

int main ()
{
    f2 <long> ();
}
Run Code Online (Sandbox Code Playgroud)

然后您会在“错误列表”窗口中看到:

错误 C2338 ... 哦不,它不是 ... test.cpp(3)

这不是很有帮助。

但是,你所寻求的信息在输出窗口(在“打造”),如果我们在那里看,我们看到:

test.cpp
g:\source\tests\test.cpp(3): error C2338: oh no it isn't
g:\source\tests\test.cpp(4): note: see reference to function template instantiation 'void f1<T>(void)' being compiled
    with
    [
        T=long
    ]
g:\source\tests\test.cpp: note: see reference to function template instantiation 'void f2<long>(void)' being compiled
Run Code Online (Sandbox Code Playgroud)

还有您要寻找的“堆栈跟踪”(您可以双击要带到相应源代码行的内容)。

就那么简单。在reextester(我所知道的唯一支持 MSVC 的在线编译器)上运行它。当人们不理解问题时,典型的 SO 会否决。