Thread: Weird behaviour of "inline if"

  1. #1
    Registered User
    Join Date
    Jun 2012
    Location
    Here
    Posts
    23

    Weird behaviour of "inline if"

    Hello,

    Can someone explain why "inline if" does not work in this particular example:

    Code:
    #include <iostream>
    using namespace std;
    
    string replaceStr(string source, char a, string b)
        {
        string result = "";
    
        for(int i=0; i<source.length(); i++)
            {
            char ch = source[i];
    
    
            // I have no idea why this doesn't work:
            // result += (ch == a) ? b : ch;
    
            if(ch == a)
                result += b;
            else
                result += ch;
            }
    
        return result;
        }
    
    
    int main()
        {
        string s = "banana";
    
        cout << s << endl; // outputs: banana
        s = replaceStr(s, 'a', "--");
        cout << s << endl; // outputs: b--n--n--
    
        return 0;
        }
    Looks like this "inline if" is really unhappy with different variable types (string and char), but if-else statement causes no errors.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I did this and it worked
    Code:
    #include <iostream>
    
    
    using namespace std;
    int main()
    {
        char a='a';
        char b='b';
    
        string result ="";
    
        result +=  (a==b)? a : b ;
    
    
        cout<<result<<endl;
    
        return 0;
    }
    Also mind that it is not very efficient to have strlen as a condition if for loop, because every time you have to check the condition, you have to call strlen again and again, while calling it only once is enough.

    So cache the result of strlen and use this in the condition.

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by heinz55 View Post
    Looks like this "inline if" is really unhappy with different variable types (string and char)
    Because it can't decide which version of the operator+= to call.
    That ambiguity is removed when you use the if statement explicitly.

    [EDIT]
    Actually, the issue seems to be more fundamental !
    Operands of the ? operator are syntactically not allowed to be of different types.
    Quote Originally Posted by g++
    operands to ?: have different types ‘char’ and ‘const char*’
    Last edited by manasij7479; 11-12-2012 at 05:49 AM.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The edit above was very helpful for me and i guess for the poster too.Bravo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-06-2010, 06:39 AM
  2. Replies: 6
    Last Post: 05-18-2003, 06:29 PM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM
  5. Ask about error when declare "static inline int"
    By ooosawaddee3 in forum C Programming
    Replies: 3
    Last Post: 05-25-2002, 05:03 AM

Tags for this Thread