No viable conversion from 'bool' to 'std::string'

后端 未结 4 2080
感动是毒
感动是毒 2021-01-29 13:44

I have some code that looks like this:

static std::string Foo(const std::string& blah)
{
   if ( someWierdEdgeCase() ){
      return false;  // <-- this l         


        
4条回答
  •  清酒与你
    2021-01-29 14:20

    If you need the return type to be - for whatever reason - not always present, return by pointer, instead of returning by value.

    static yourType* Foo(const std::string& blah){
       if ( someWierdEdgeCase() ){
          return 0;
       }
    }
    

    Then you can test and assign the function in the same line

    yourType* result;
    if(result = Foo("something")){
      // the result was correct
    

    Of course - since your function returns a std::string, you can return an empty string (""), or - also independent of the return type - throw an exception.

提交回复
热议问题