inlining and copy constructors

This is a discussion on inlining and copy constructors within the C++ Programming forums, part of the General Programming Boards category; if I have a templated inline function that takes an object by value i.e. Code: template <typename T> void DoSomething(T ...

  1. #1
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597

    inlining and copy constructors

    if I have a templated inline function that takes an object by value
    i.e.
    Code:
    template <typename T>
    void DoSomething(T someT)
    {
        // assume << is defined!!
        cout << someT;
    }
    if this function is called and inlined will T's copy ctor still get called?
    i.e.
    Code:
    ExpseniveToCopy etc;
    DoSomething(etc);
    
    //becomes either
    ExpseniveToCopy etc;
    cout <<etc;
    
    // or
    
    ExpseniveToCopy etc;
    ExpseniveToCopy someT(etc);
    cout << someT;
    I presume it's the latter, but can't find anything definite (and I can't use a reference, as DoSomething sometimes accepts a reference and reference to a reference is illegal)
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    (and I can't use a reference, as DoSomething sometimes accepts a reference and reference to a reference is illegal)
    Hmm, is that so? I thought it should work. Do you mean something like this?

    Code:
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    
    template <typename T>
    void DoSomething(T& someT){
      cout << someT;
    }
    
    int main(int argc, char *argv[])
    {
      int a = 2;
      int& b = a ;
      DoSomething( b );
      cin.get();
      return 0 ;
    }

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    allow me to rephrase. ref to ref may or may not be illegal but VS.net2k3 spits out the following error

    error C2529 : reference to reference is illegal
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Yes a reference to a reference would be illegal and it makes no sense. However you can initalize a reference with an object that is a reference.

    Code:
    int a = 5;
    int& b = a;
    int&& c = b; // not legal
    Code:
    int a = 5;
    int& b = a;
    int& c = b; // legal

  5. #5
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Thantos
    Yes a reference to a reference would be illegal and it makes no sense. However you can initalize a reference with an object that is a reference.
    the problem is that sometimes the type can't be deduced so you have to specify it (this happens specifically with mem_fun and bind2nd)

    check out this thread on gamedev.net for more info.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok I'm still not seeing your problem. Can you show me some actual code that invokes this behavior?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You might consider asking this question on comp.lang.c++(.moderated) or the CodeGuru forums. They have a lot more active advanced users than there are here.

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by ChaosEngine
    if this function is called and inlined will T's copy ctor still get called?
    It's simple enough to try yourself.
    Code:
    #include <iostream>
    
    class A
    {
    public:
      A() {}
      A(const A& obj) {std::cout
        << "Copy Constructor" << std::endl;}
    };
    
    template <typename T>
    inline void DoSomething(T someT) {}
    
    int main()
    {
      A a;
      DoSomething( a );
      return 0 ;
    }
    Quote Originally Posted by ChaosEngine
    allow me to rephrase. ref to ref may or may not be illegal but VS.net2k3 spits out the following error

    error C2529 : reference to reference is illegal
    Good, you gave us half of it. Now, let's see the code that generates this error.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc and inlining
    By Laserve in forum C Programming
    Replies: 4
    Last Post: 11-24-2006, 03:31 AM

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