views:

1219

answers:

5

Hi all, I was recently trying to gauge my operator overloading/template abilities and as a small test, created the Container class below. While this code compiles fine and works correctly under MSVC 2008 (displays 11), both MinGW/GCC and Comeau choke on the operator+ overload. As I trust them more than MSVC, I'm trying to figure out what I'm doing wrong.

Here is the code:

#include <iostream>

using namespace std;

template <typename T>
class Container
{
      friend Container<T> operator+ <> (Container<T>& lhs, Container<T>& rhs);
   public: void setobj(T ob);
     T getobj();
      private: T obj;
};

template <typename T>
void Container<T>::setobj(T ob)
{
   obj = ob;
}

template <typename T>
T Container<T>::getobj()
{
   return obj;
}

template <typename T>
Container<T> operator+ <> (Container<T>& lhs, Container<T>& rhs)
{
      Container<T> temp;
      temp.obj = lhs.obj + rhs.obj;
      return temp;
}

int main()
{    
    Container<int> a, b;

 a.setobj(5);
    b.setobj(6);

 Container<int> c = a + b;

 cout << c.getobj() << endl;

    return 0;
}

This is the error Comeau gives:

Comeau C/C++ 4.3.10.1 (Oct  6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
Copyright 1988-2008 Comeau Computing.  All rights reserved.
MODE:strict errors C++ C++0x_extensions

"ComeauTest.c", line 27: error: an explicit template argument list is not allowed
          on this declaration
  Container<T> operator+ <> (Container<T>& lhs, Container<T>& rhs)
               ^

1 error detected in the compilation of "ComeauTest.c".

I'm having a hard time trying to get Comeau/MingGW to play ball, so that's where I turn to you guys. It's been a long time since my brain has melted this much under the weight of C++ syntax, so I feel a little embarrassed ;).

Thanks in advance.

EDIT: Eliminated an (irrelevant) lvalue error listed in initial Comeau dump.

+4  A: 

I found the solution thanks to this forum posting. Essentially, you need to have a function prototype before you can use 'friend' on it within the class, however you also need the class to be declared in order to properly define the function prototype. Therefore, the solution is to have two prototype definitons (of the function and the class) at the top. The following code compiles under all three compilers:

#include <iostream>

using namespace std;

//added lines below
template<typename T> class Container;
template<typename T> Container<T> operator+ (Container<T>& lhs, Container<T>& rhs); 

template <typename T>
class Container
{
      friend Container<T> operator+ <> (Container<T>& lhs, Container<T>& rhs);
      public: void setobj(T ob);
        T getobj();
      private: T obj;
};

template <typename T>
void Container<T>::setobj(T ob)
{
      obj = ob;
}

template <typename T>
T Container<T>::getobj()
{
      return obj;
}

template <typename T>
Container<T> operator+ (Container<T>& lhs, Container<T>& rhs)
{
      Container<T> temp;
      temp.obj = lhs.obj + rhs.obj;
      return temp;
}

int main()
{    
    Container<int> a, b;

    a.setobj(5);
    b.setobj(6);

    Container<int> c = a + b;

    cout << c.getobj() << endl;

    return 0;
}
GRB
+2  A: 
template <typename T>
Container<T> operator+ <> (Container<T>& lhs, Container<T>& rhs)

Here the "<>" after operator+ should be removed since you're just declaring a new template, not specializing a general one. Also at least g++ wants to see the template declaration before the friend declaration, so it needs to be moved before the declaration of Container. So the following order of the declarations works:

// forward declaration of Container<T>
template <typename T>
class Container;

template <typename T>
Container<T> operator+(Container<T>& lhs, Container<T>& rhs)
{ ... }

template <typename T>
class Container
{
      friend Container<T> operator+ <> (Container<T>& lhs, Container<T>& rhs);
      ...
};
sth
A: 

'operator+' is not a member function, and it's not templated. It's just the operator+ which takes templated parameters.'

 template <typename T>
 Container<T> operator+ (Container<T>& lhs, Container<T>& rhs)
James Curran
I think the operator+ is a templated function, is it not? A Container<int> and a Container<double> will be two different types, so the compiler will need to generate two different operator+ functions to deal with them. Doesn't that mean operator+ is a templated function?
ryan_s
It *is* a function template. But you're correct in omitting the "<>" from the definition of the template. ("<>" is only used when specifying a function template *to call*, and even then is only necessary when a non-template function of the same name exists.)
j_random_hacker
+1  A: 

I gave this a shot under GCC and got it to compile and run with a few changes. There were two changes I had to make to get GCC happy.

One was the declaration of the friend template function. It is its own template declaration separate from the class one, so I used U instead of the Container class' T there. I also got rid of the <> after the operator+. I don't think you'd need those unless you're writing a template specialization.

      template<typename U>
      friend Container<U> operator+ (Container<U>& lhs, Container<U>& rhs);

Two, the line

Container<int>& c = a + b;

didn't fly with GCC because you're asking to store a reference to a temporary (the result of the addition). I removed the ampersand so that there is a place to store the result.

I saw your post just now, that also works because of the forward declarations. I guess I'll post this anyway as an alternative that doesn't require them. Of course I only tested in GCC...

ryan_s
I found this in another posting online and this would be my personal preference of the two (more concise).
GRB
This one will grant access to all template specializations (so operator+<int> could access privates of Container<float>). The code of the asker wants to grant access only to operator+<T> (only operator+<float> can access the privates of Container<float>)
Johannes Schaub - litb
@litb: I saw that issue mentioned as well, but I'm having trouble setting up such a scenario. Could you provide an example code snippet where the int overload accesses a float's private members?
GRB
@GRB, you can always construct such cases artificially. Whether or not this happens on reality in your code is not so much of a question. The mere possibility that when it happens (by accident), there won't be an error message is enough for it to rule it out. The idea of concepts like "const" and "private" and "friend" is to document dependencies and at the same time keep things as hidden and loose as possible.
Johannes Schaub - litb
I understand that the code as is may not cause this to happen, what I'm having trouble with is creating such an artificial case. I can't personally think of one, so I was wondering if you could.
GRB
Johannes Schaub - litb
ah, now I see what you're saying; I can see how this can be exploited. Thanks for illustrating that.
GRB
+1  A: 

You'd be better off defining the function directly in the class. Also, you should pass the parameters as const references.

template <typename T>
class Container
{
public:
    friend Container operator+ (Container const & lhs, Container const & rhs)
    {
        // ...
    }
};
avakar