What's the best way to force the user of a C++ function to acknowledge the semantic meaning of parameters that are numerical constants?

后端 未结 7 2099
故里飘歌
故里飘歌 2021-02-13 15:27

I\'d like to write function interfaces that force the user to acknowledge the semantic meaning of built-in constants. For example, I\'d like to take

void rotate(         


        
7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-13 15:53

    Slower is in the eye of the beholder. How often do you call this code? Yes, you'll be creating bigger items on the stack when passing a Radians class than a primitive float.

    Why is this important? At some point the user of your class has to have half a brain and understand the API. You could declare it as

    void rotate(float radians);
    

    Alternately, assuming you don't actually modify the angle, you could pass your Radians class by const reference:

    void rotate(const Radians &angle);
    

    which would presumably be faster than passing by value.

提交回复
热议问题