Jon*_*201 2 c++ std c++-standard-library libc++ c++17
对于std::is_literal_type和,这是相同的情况std::is_standard_layout.
std::is_literal_type在libc ++中的实现是
template <class _Tp> struct _LIBCPP_TEMPLATE_VIS is_literal_type
#ifdef _LIBCPP_IS_LITERAL
: public integral_constant<bool, _LIBCPP_IS_LITERAL(_Tp)>
#else
: integral_constant<bool, is_scalar<typename remove_all_extents<_Tp>::type>::value ||
is_reference<typename remove_all_extents<_Tp>::type>::value>
#endif
{};
Run Code Online (Sandbox Code Playgroud)
没有_LIBCPP_IS_LITERAL,所以代码将是
template <typename T> struct is_literal_type : integral_constant<bool,
is_scalar<typename remove_all_extents<T>::type>::value or
is_reference<typename remove_all_extents<T>::type>::value> {};
Run Code Online (Sandbox Code Playgroud)
我写了一个演示:
#include <iostream>
using namespace std;
struct s {
int a;
char b;
long c;
};
int main(int argc, char *argv[]) {
cout << boolalpha;
cout << is_scalar_v<typename remove_all_extents<s>::type> << endl;
cout << is_reference_v<typename remove_all_extents<s>::type> << endl;
}
Run Code Online (Sandbox Code Playgroud)
结果是false和false.但结果is_literal_type_v<s>是true.
任何人都能解释一下std::is_literal_type有效吗
is_literal_type是一个"神奇"的C++库.它无法在C++中实现,因为当前的语言(使用静态反射,它应该是可能的,但最快的是C++ 23).它是通过使用特定于编译器的内部工具实现的,而不是直接使用C++实现的._LIBCPP_IS_LITERAL可能是由编译器定义的宏(因此它看起来未定义),它表示特定的编译器内在函数.
因此,你真的不应该过分关注这个或许多其他类型特征的实现.
我想,_LIBCPP_IS_LITERAL未定义的版本是为了与没有公开必要内在函数的旧版本编译器兼容.因此,如果没有编译器支持,库实现可以做到最好.