Prevent return function if parameter condition is true

as the title says, I was wondering if the execution of the function could be stopped. In my specific case, I try to make the [] operator and prevent the utility from using it if the value specified by the parameters is too large:

in .h:

class Vec4 { float x,y,z,w; public: float operator[](const unsigned int i); } 

in .cpp:

 float Vec4::operator[](const unsigned int i) { if(i == 0) return x; if(i == 1) return y; if(i == 2) return z; if(i == 3) return w; } 

I would like to “break up” the function if I> = 4 At the moment I'm just doing console output and returning 0.0f

thanks to show me if there is a way ... or not!

+6
source share
4 answers

You can do at least 4 things.

  • Returns the known error value from the function. E.g. -1 if the input value does not fit.
  • Raise an exception.
  • Change the function to pass the output by reference and enter the error code.
  • Help the user get a point with a strongly typed enum class .

Option 1

 float Vec4::operator[](const unsigned int i) { switch (i) case 0: return x; ... default: return nan; 

Option 2

 default: throw InvalidInputException; 

Option 3

 typedef ErrCode int; const int ERROR = -1; const int SUCCESS = 1; ... ErrCode Vec4::getPoint(const unsigned int i, float &ouptut) { ... switch (i) case 0: output = x; return SUCCESS; default: return ERROR; 

Option 4 (C ++ 11)

 class Vec4 { ... public: enum class VecMem {X, Y, Z, W}; float Vec4::getPoint(VecMem member) { switch (member): case X: return x; ... 

Using:

 Vec4.getPoint(Vec4::VecMem::X); 
+7
source

If your argument is known at compile time, you can use static_assert .

 #include <cassert>; constexpr float Vec4::operator[](const unsigned int i) { static_assert(i <= 3); ... } 

Static statements do not affect performance because they are checked at compile time.

If your argument is not known at compile time, you can use dynamic assert .

 #include <cassert>; float Vec4::operator[](const unsigned int i) { assert(i <= 3); ... } 

Dynamic statements are contained in the compiler output only after setting the compiler flags that enable them, so they can be easily disabled.

Or you can just throw an exception .

 float Vec4::operator[](const unsigned int i) { if (i > 3) { throw BadArgumentException("i must be <= 3"); } ... } 

Exceptions can contain a lot of information and be processed in other parts of the code, but have the greatest performance.

http://en.cppreference.com/w/cpp/error/assert

0
source

Use the "case switch" and for the case where the différent value is greater than 3, returns the default state, which may be the specific value reserved for this case

0
source

If you don't mind using upcoming functions, you can use std::optional to do this:

 std::optional<float> Vec4::operator[](const unsigned int i) { if(i == 0) return x; if(i == 1) return y; if(i == 2) return z; if(i == 3) return w; return {}; } 

The disadvantage is that the caller must check the correctness of the return value (using the member function operator bool or has_value ) or use the member function value_or to get the default value for empty options.

0
source

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


All Articles