derived class as default argument g++
Asked Answered
W

2

6

Please take a look at this code:

template<class T>
class A
{
 class base
 {

 };

 class derived : public A<T>::base
 {

 };

public:

 int f(typename A<T>::base& arg = typename A<T>::derived())
 {
  return 0;
 }
};

int main()
{
 A<int> a;
 a.f();
 return 0;
}

Compiling generates the following error message in g++:

test.cpp: In function 'int main()':
test.cpp:25: error: default argument for parameter of type
                    'A<int>::base&' has type 'A<int>::derived'

The basic idea (using derived class as default value for base-reference-type argument) works in visual studio, but not in g++. I have to publish my code to the university server where they compile it with gcc. What can I do? Is there something I am missing?

Wheeling answered 16/4, 2010 at 13:19 Comment(1)
Not solving the error, but you can just write int f(base& arg = derived()).Lifework
L
7

You cannot create a (mutable) reference to an r-value. Try to use a const-reference:

 int f(const typename A<T>::base& arg = typename A<T>::derived())
//     ^^^^^

Of course you can't modify arg with a const-reference. If you have to use a (mutable) reference, use overloading.

 int f(base& arg) {
   ...
 }
 int f() {
   derived dummy;
   return f(dummy);
 }
Lifework answered 16/4, 2010 at 13:25 Comment(2)
Thank you all, this solved my problem. The const version was enough, in the real context, base is a predicate class to compare things, so I don't need to modify it.Braden
You've been tripped by a Visual Studio extension, compile with Warning Level 4 on and it should fire on this.Housley
B
4

The problem you are facing is that you cannot use a temporary as default argument to a function taking a non-const reference. Temporaries cannot be bound to non-const references.

If you are not modifying the object internally, then you can just change the signature to:

int f(typename A<T>::base const & arg = typename A<T>::derived())

If you are actually modifying the passed in argument, you must use some other technique to allow for optional arguments, simplest of which would be using a pointer that can be defaulted to NULL.

Bronchial answered 16/4, 2010 at 13:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.