Home > Backend Development > C++ > body text

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

Linda Hamilton
Release: 2024-10-31 04:07:53
Original
897 people have browsed it

 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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!