I am using Clang 5 on Windows via clang-cl and have run into a problem trying to turn off runtime type information (-fno-rtti
) when using std::function
.
Here's my example that won't compile:
#include <functional>
void foo(std::function<void()> ra2)
{
}
int main()
{
auto bar = []()
{
};
foo(bar);
return EXIT_SUCCESS;
}
Command line:
clang-cl test.cpp -Xclang -fno-rtti
The error is:
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));
^
This surprises me, is there a way to use std::function
with lambdas in Clang without RTTI? The docs say that only target
and target_type
on std::function
should need RTTI. I can roll my own version of std::function
, but it seems a shame to have to do so.
It works fine if I use MSVC with compiler flag /GR-
.
This is a bug fixed in Clang 13.
The Clang MSVC driver fails to define _HAS_STATIC_RTTI
to 0
, which is used by the MSVC standard library to enable no-RTTI code.
As a workaround you can manually define it globally using compiler flags or defining it before including any standard library headers.