Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is the c++ code in standard library all valid c++?

Just out of curiosity, I looked at how std::is_pointer is implemented and saw stuff like this (one of multiple overloads):

template <class _Ty>
_INLINE_VAR constexpr bool is_pointer_v<_Ty*> = true; 

which if I copy and paste into my code and compile says constexpr is not valid here. What is happening?

Is it possible to implement structs like std::is_pointer and std::is_reference by us? (Not that I would, just curious so please don’t come at me)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

MRE with msvc 2019:

template <class _Ty>
inline constexpr bool is_pointer_v<_Ty*> = true;

int main()
{
}

Error: error C7568: argument list missing 
after assumed function template 'is_pointer_v'

>Solution :

You are using a partial template specialization here. You need a complete declaration of the template to get that code to compile, like this:

template <class _Ty>
inline constexpr bool is_pointer_v = false;

template <class _Ty>
inline constexpr bool is_pointer_v<_Ty*> = true;

See here for example code

To answer your questions, the STL implementation requires specific C++ primitives that the compiler implements to support the required API. You can’t have a constexpr version of the STL if the compiler does not implement it.

It’s not possible to implement the complete STL without some compiler specifics code and the operating system’s primitives. This is different from system to system (you can’t use the STL implementation of linux on windows for example).

Yet, you can always copy & paste the STL code from your system and it’ll always build correctly on your compiler.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading