Home  >  Article  >  Backend Development  >  Why is there no `is_complete` template in Boost.TypeTraits?

Why is there no `is_complete` template in Boost.TypeTraits?

Linda Hamilton
Linda HamiltonOriginal
2024-10-31 04:07:53897browse

 Why is there no `is_complete` template in Boost.TypeTraits?

Is_Complete Template in Boost Library

While seeking an is_complete template in the Boost library, it became evident that such a template does not exist within Boost.TypeTraits. This raises questions about the rationale behind its absence and the potential design of such a template.

Consider the following code snippet:

<code class="cpp">//! Check whether type complete
template<typename T>
struct is_complete
{   
  static const bool value = ( sizeof(T) > 0 );
};

...

// use it in such a way
BOOST_STATIC_ASSERT( boost::is_complete<T>::value );</code>

This code is flawed as applying sizeof to an incomplete type is illegal. Therefore, an alternative solution is sought.

One possible approach involves the use of SFINAE. However, this poses a limitation as it cannot generally solve the problem without violating the One Definition Rule (ODR).

A platform-specific solution proposed by Alexey Malistov is available for MSVC with a slight modification:

<code class="cpp">namespace 
{
    template<class T, int discriminator>
    struct is_complete {  
      static T &amp; getT();   
      static char (&amp; pass(T))[2]; 
      static char pass(...);   
      static const bool value = sizeof(pass(getT()))==2;
    };
}
#define IS_COMPLETE(X) is_complete<X,__COUNTER__>::value</code>

Unfortunately, the use of the __COUNTER__ macro is not standardized, potentially limiting its applicability across different compilers.

The above is the detailed content of Why is there no `is_complete` template in Boost.TypeTraits?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn