clang、std::function 和 -fno-rtti

kei*_*ith 5 c++ clang c++11 clang-cl

我正在通过 clang-cl 在 Windows 上使用 Clang 5,并且-fno-rtti在使用std::function.

这是我无法编译的示例:

#include <functional>

void foo(std::function<void()> ra2)
{
}

int main()
{
    auto bar = []()
    {
    };

    foo(bar);

    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

命令行:

clang-cl test.cpp -Xclang -fno-rtti
Run Code Online (Sandbox Code Playgroud)

错误是:

C:\Program Files (x86)\Microsoft Visual Studio\Preview\Community\VC\Tools\MSVC\14.13.26128\include\functional(435,11):  error:
      cannot use typeid with -fno-rtti
                return (typeid(_Callable));
                        ^
Run Code Online (Sandbox Code Playgroud)

这让我感到惊讶,有没有办法std::function在没有 RTTI 的情况下在 Clang 中使用lambdas?该文件说,只有targettarget_typestd::function应该需要RTTI。我可以推出自己的 版本std::function,但不得不这样做似乎很可惜。

如果我将 MSVC 与编译器标志一起使用,它工作正常/GR-

tam*_*bre 5

这是Clang 13 中修复的错误。


Clang MSVC 驱动程序无法定义_HAS_STATIC_RTTIto 0,MSVC 标准库使用它来启用 no-RTTI 代码。

作为一种解决方法,您可以使用编译器标志手动定义它,或者在包含任何标准库头文件之前定义它。