Overloading member methods with typedef aliases as parameters

后端 未结 3 460
我寻月下人不归
我寻月下人不归 2021-01-19 12:49

I’m having some trouble overloading methods in C++.

typedef char int8_t;
class SomeClass{
public:
…
void Method(int8_t paramater);
void Method(char paramater         


        
3条回答
  •  长发绾君心
    2021-01-19 13:55

    You might gain some degree of improvement by trying this:

    void Method(char paramater);
    void Method(signed char paramater);
    void Method(unsigned char paramater);
    

    If an implementation defines int8_t, and if the definition matches one of those three, then the correct function will get called.

    However, a devious implementation could do something like this:

    typedef __special_secret_sauce int8_t;
    

    and then you would need to define another overload for int8_t. It's pretty tough for you to define another overload for int8_t to contend with those implementations and at the same time not define it for implementations that typedef int8_t as signed char. Someone else said it's not even possible.

    There can be implementations where int8_t doesn't exist at all. If you just define overloads for the three variations of char then you'll have no problem there.

提交回复
热议问题