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

C++ Add constraints to member function of template class

I have a 3D vector template class and I want to add a normalize member function to it.

Normalizing a vector only makes sense, if they use floating point numbers.

I want to use the c++20 requires syntax with the std::floating_point concept.

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

template<typename Type>
class vector3 {

  [...]

  vector3& normalize() requires std::floating_point<Type> noexcept;

}

But the compiler (gcc 11.2.0) gives me an error

error: expected ';' at end of member declaration
  242 |   vector3& normalize() requires (std::floating_point<Type>) noexcept;
      |                                                          ^
      |                                                          ;

>Solution :

noexcept is part of declarator, and requires-clause must come after declarator (link)

so you need to write

// from @Osyotr in comment
vector3 & normalize() noexcept requires std::is_floating_point_v<Type>; 
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