How is std :: is_empty <T> implemented in VS2015 (or any compiler)?

My current question was inspired by an attempt to understand how std::unique_ptr<T, D>template mechanics are used to create an instance of a template class the size T*when D(the type of deletion) is the type of the lambda function, but larger than size when Dit is the type of the function pointer (since space needs to be allocated in the instance unique_ptrto hold the pointer functions).

Looking through the VS2015 source code, I found what std::unique_ptrcomes from std::_Unique_ptr_base, which in turn declares a member of the data type _Compressed_pair<class _Ty1, class _Ty2, bool = is_empty<_Ty1>::value && !is_final<_Ty1>::value>. The type _Ty1in this last context is the type of the deleter D, which is the second template parameter unique_ptrnoted in the previous paragraph; those. The motivation for this question is that I contrast the _Ty1lambda type, and the _Ty1type of the function pointer. (Actually, the default value is used bool.)

I acknowledge what it is_empty<_Ty1>::valueis truewhen it _Ty1is an instance of the lambda type (when the lambda has no capture variables and therefore has a size of 0); but this falseis when _Ty1is the type of function pointer.

This led me to determine how std::is_empty.

Ugh!

std::is_empty, VS2015 ++.

type_traits :

// TEMPLATE CLASS is_empty
template<class _Ty>
struct is_empty _IS_EMPTY(_Ty)
{   // determine whether _Ty is an empty class
};

... _IS_EMPTY :

#define _IS_EMPTY(_Ty)  \
: _Cat_base<__is_empty(_Ty)>

... , __is_empty . GREPped VS2015 ( , , ++, , , ).

++, . ... , ( intrinsics), ... - .

- ? std::is_empty<T> VS2015 , , - ?

+4
2

, MSV++ __isempty(T), . T, std::is_empty<T>, final, , , .

, T , , - <<25 > , std::is_empty<T> true):

template <bool, typename T>
struct is_empty_aux: T { unsigned long long dummy; };
template <typename T>
struct is_empty_aux<false, T> { unsigned long long dummy[2]; };

template <typename T>
struct is_empty:
    std::integral_constant<bool,
                           sizeof(is_empty_aux<std::is_class<T>::value, T>)
                           == sizeof(unsigned long long)> {
};

, T - final, is_empty_aux . final std::is_final<T>, , . , . , . - /, - : . , .

+6

gcc 4.8 Ubuntu 14.04. , __is_empty, __is_pod, __is_polymorphic ..,

// /usr/include/c++/4.8/type_traits:516
template<typename _Tp>
struct is_empty
    : public integral_constant<bool, __is_empty(_Tp)>
{};

, , ++. g++ clang++

#include <stdio.h>

struct X {};

int main()
{
    printf("%d %d %d\n", __is_empty(X), __is_enum(X), __is_class(X));
    return 0;
}

, , , , , ( , X x; __is_empty(x);).

+1

Source: https://habr.com/ru/post/1629730/


All Articles