Like Tree2Likes
  • 2 Post By manasij7479

Weird behaviour of "inline if"

This is a discussion on Weird behaviour of "inline if" within the C++ Programming forums, part of the General Programming Boards category; Hello, Can someone explain why "inline if" does not work in this particular example: Code: #include <iostream> using namespace std; ...

  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,681
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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 04:49 AM.
    Salem and rags_to_riches like this.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    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, 07: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, 06: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21