Home > Enterprise >  cannot overload functions distinguished by return type alone but it is not a real mistake
cannot overload functions distinguished by return type alone but it is not a real mistake

Time:04-16

I have different variants of some function, that are choosed by preprocessor definition

    #if defined(V2)
        bool getAICoord(TTT_Game& game) {
    
            //
            // 4x4 field
            //
    
            return false;
        }
    #elif defined(V3)
        bool getAICoord(TTT_Game& game) {
    
            //
            // renju field
            //
    
            return false;
        }
    #else // V1 
        bool getAICoord(TTT_Game& game) {
            // some code
            return false;
        }
    #endif

And it compiles well, but IntelliSense gives me error

cannot overload functions distinguished by return type alone

I know, that it is not perfect, but is there any way to exclude this one function from its checklist or something like this?

CodePudding user response:

You could workaround this error by only typing the function signature once and using the preprocessor definitions on the body of the function; e.g.

bool getAICoord(TTT_Game& game) {
#if defined(V2)
    //
    // 4x4 field
    //
    return false;
#elif defined(V3)
    //
    // renju field
    //
    return false;
#else // V1
    // some code
    return false;
#endif
}

CodePudding user response:

Preprocessor macros notoriously confuse IDEs, as they are pure text transformation of the code, instead of being part of the language itself.

Instead of using macros, if constexpr lets you do conditional compilation as part of the language itself instead of doing it as a preprocessor step:

This has a number of advantages:

  • It confuses IDEs a lot less.
  • All code paths get syntactical validation, even when excluded.
constexpr int kVersion = SOME_DEFINE;

bool getAICoord(TTT_Game& game) {
  if constexpr(kVersion == 3) {
      //...    
  } else if constexpr(kVersion == 2) {
      //...
  } else {
      //...
  }
}
  • Related