Zugzwang
October 20th, 2009, 06:27 PM
I must say that I don't see any mistake in the following program:
#include <iostream>
// #include <boost/utility.hpp>
class A /* : public boost::noncopyable */ {
private:
A& operator=(const A& b) {
std::cout << "Assignment operator" << std::endl;
return *this;
}
public:
A() {
std::cout << "Constructor" << std::endl;
}
A(const A &b) {
std::cout << "Copy constructor" << std::endl;
}
};
int main()
{
A a(A());
return 0;
}
user@user-laptop:/tmp$ g++ -pedantic -Wall test.cpp
user@user-laptop:/tmp$ ./a.out
user@user-laptop:/tmp$ g++ --version
g++ (Ubuntu 4.3.3-5ubuntu4) 4.3.3
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
It compiles fine. When running, exactly nothing happens. This is surprising, as at least the constructor and the destructor should be called. Having the class A deriving from boost::noncopyable doesn't change anything, so the compiler shouldn't call any automatically synthesized function. Also, changing the line "A a(A());" to "A a(A);" does not change anything.
Indeed, when looking at the generated assembler code, nothing is called.
It might be the case that the compiler thinks that "A a(A())" is a function declaration. However, the line "A a = A(A());" makes only one constructor being called and not the copy constructor, which is again problematic.
#include <iostream>
// #include <boost/utility.hpp>
class A /* : public boost::noncopyable */ {
private:
A& operator=(const A& b) {
std::cout << "Assignment operator" << std::endl;
return *this;
}
public:
A() {
std::cout << "Constructor" << std::endl;
}
A(const A &b) {
std::cout << "Copy constructor" << std::endl;
}
};
int main()
{
A a(A());
return 0;
}
user@user-laptop:/tmp$ g++ -pedantic -Wall test.cpp
user@user-laptop:/tmp$ ./a.out
user@user-laptop:/tmp$ g++ --version
g++ (Ubuntu 4.3.3-5ubuntu4) 4.3.3
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
It compiles fine. When running, exactly nothing happens. This is surprising, as at least the constructor and the destructor should be called. Having the class A deriving from boost::noncopyable doesn't change anything, so the compiler shouldn't call any automatically synthesized function. Also, changing the line "A a(A());" to "A a(A);" does not change anything.
Indeed, when looking at the generated assembler code, nothing is called.
It might be the case that the compiler thinks that "A a(A())" is a function declaration. However, the line "A a = A(A());" makes only one constructor being called and not the copy constructor, which is again problematic.