A
Andy Buckley
Hi,
A friend and I have recently had trouble getting code to compile when
using temporary objects in constructors. A minimal example is below:
This code fragment is used to construct seven ROD objects and run
their doSomething() method (which is trivial).
As far as we can tell, all seven methods should successfully construct a
ROD object using temporary objects as constructor parameters, but using
g++ 3.2.3 only the first 4 actually succeed. The remainder seem to be
treated as constructing/declaring(?) a function pointer and so the attempt
to call a method fails for these.
Confusingly, some of the approaches which fail under g++ do compile
successfully using MicroSoft Visual C++ 6.0: can anyone enlighten us as to
whether this is a failing in our understanding of the language or a bug in
g++? (probably the first!)
// Minimal test case:
#include <iostream>
typedef unsigned int U;
class T {
public:
T() {};
T( const U & ) { }
};
class ROD {
public:
ROD(const T &) { }
void doSomething() const {
std::cout << "Hello -- I am a ROD" << std::endl;
}
};
class CC {
public:
void doSomethingElse() {
ROD rod1 = ROD( T( t() ) ) ; // does what I want -- makes a ROD
ROD rod2 ( T( this->t() ) ) ; // does what I want -- makes a ROD
ROD rod3 ( *(new T() ) ) ; // does what I want -- makes a ROD
ROD rod4 ( T( 0 ) ) ; // does what I want -- makes a ROD
rod1.doSomething(); // succeeds rod2.doSomething();
// succeeds rod3.doSomething(); // succeeds
rod4.doSomething(); // succeeds
/*
ROD rod5 ( T( t() ) ) ; // doesn't do what I want (*) ROD
rod6 ( ROD( T( t() ) )); // doesn't do what I want (*) ROD
rod7 ( T() ) ; // doesn't do what I want (*)
rod5.doSomething(); // doesn't compile
rod6.doSomething(); // doesn't compile
rod7.doSomething(); // doesn't compile
*/
// (*) At these statements, funtion pointers seem to be declared
}
T t() const { return T(0); }
};
int main () {
CC cc;
cc.doSomethingElse();
return 0;
}
A friend and I have recently had trouble getting code to compile when
using temporary objects in constructors. A minimal example is below:
This code fragment is used to construct seven ROD objects and run
their doSomething() method (which is trivial).
As far as we can tell, all seven methods should successfully construct a
ROD object using temporary objects as constructor parameters, but using
g++ 3.2.3 only the first 4 actually succeed. The remainder seem to be
treated as constructing/declaring(?) a function pointer and so the attempt
to call a method fails for these.
Confusingly, some of the approaches which fail under g++ do compile
successfully using MicroSoft Visual C++ 6.0: can anyone enlighten us as to
whether this is a failing in our understanding of the language or a bug in
g++? (probably the first!)
// Minimal test case:
#include <iostream>
typedef unsigned int U;
class T {
public:
T() {};
T( const U & ) { }
};
class ROD {
public:
ROD(const T &) { }
void doSomething() const {
std::cout << "Hello -- I am a ROD" << std::endl;
}
};
class CC {
public:
void doSomethingElse() {
ROD rod1 = ROD( T( t() ) ) ; // does what I want -- makes a ROD
ROD rod2 ( T( this->t() ) ) ; // does what I want -- makes a ROD
ROD rod3 ( *(new T() ) ) ; // does what I want -- makes a ROD
ROD rod4 ( T( 0 ) ) ; // does what I want -- makes a ROD
rod1.doSomething(); // succeeds rod2.doSomething();
// succeeds rod3.doSomething(); // succeeds
rod4.doSomething(); // succeeds
/*
ROD rod5 ( T( t() ) ) ; // doesn't do what I want (*) ROD
rod6 ( ROD( T( t() ) )); // doesn't do what I want (*) ROD
rod7 ( T() ) ; // doesn't do what I want (*)
rod5.doSomething(); // doesn't compile
rod6.doSomething(); // doesn't compile
rod7.doSomething(); // doesn't compile
*/
// (*) At these statements, funtion pointers seem to be declared
}
T t() const { return T(0); }
};
int main () {
CC cc;
cc.doSomethingElse();
return 0;
}