如何在 Visual Studio 中查看 static_assert 的 'failure source'?

How to see the 'failure source' for static_assert in Visual Studio?

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

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

我明白你的意思,因为我也使用 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> ();
}

然后你在 'Error List' window 中看到这个:

Error C2338 ... oh no it isn't ... test.cpp(3)

这不是很有帮助。

但是,您在 中查找的信息 在输出 window 中(在 'build' 下),如果我们在那里查看,我们会看到:

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

还有你要找的'stack trace'(你可以双击要取的东西到相应的源码行)

就这么简单。 运行 它位于 rextester(我知道的唯一支持 MSVC 的在线编译器)。当人们不理解这个问题时,典型的 SO 会投反对票。