No.
The implementation is allowed to create a temporary object X(2)
then call the copy constructor to copy the temporary into x2
but it isn't required to do so.
elide the copy constructor and call X(int) to initialize x2 directly
The class library developer need not define a copy constructor.
The implementation will provide one but might not ever call it.
But, if the class library developer provides a copy constructor,
it must be public even if it is elided in the definition of x2 above.
What "No"?!
I am tired of your "No!" that stems from your lack of knowledge, Bob.
You left Kurt and me with the impression
that the copy constructor *must* be invoked.
Read the Standard, section 8.5, paragraph 14, forth bullet, third sub-
bullet, beginning with the words "Otherwise (i.e., for the remaining".
If the conversion cannot be done or is ambiguous, the initialisation is
ill-formed. IOW,
struct X {
X(int);
private:
X(const X&);
};
int main() {
X x = 2;
}
should NOT compile, even with your favourite g++
#include <iostream>
class X {
private:
// representation
int I;
public:
// operators
friend
std:
stream& operator<<(std:
stream& os, const X& x) {
return os << x.I;
}
X& operator=(const X& x) {
I = x.I;
std::clog << "X:
perator=(const X& x)" << std::endl;
return *this;
}
// constructors
X(int i = 0): I(i) {
std::clog << "X::X(int), I = " << I << std::endl;
}
~X(void) {
std::clog << "X::~X(void), I = " << I << std::endl;
}
private:
X(const X& x): I(x.I) {
std::clog << "X::X(const X&)" << std::endl;
}
};
int main(int argc, char* argv[]) {
X x1(1);
std::cout << "x1 = " << x1 << std::endl;
X x2 = 2;
std::cout << "x2 = " << x2 << std::endl;
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc
main.cc: In function `int main(int, char**)':
main.cc:26: error: `X::X(const X&)' is private
main.cc:34: error: within this context
main.cc:26: error: `X::X(const X&)' is private
main.cc:34: error: within this context
main.cc:34: error: initializing temporary \
from result of `X::X(int)'
mv main.cc main.ccold
cp main.ccold main.cc
vi main.cc
diff main.ccold main.cc
25d24
< private:
g++ -Wall -ansi -pedantic -o main main.cc
./main
X::X(int), I = 1
x1 = 1
X::X(int), I = 2
x2 = 2
X::~X(void), I = 2
X::~X(void), I = 1
vi main.cc
diff main.ccold main.cc
25,28d24
< private:
< X(const X& x): I(x.I) {
< std::clog << "X::X(const X&)" << std::endl;
< }
g++ -Wall -ansi -pedantic -o main main.cc
./main
X::X(int), I = 1
x1 = 1
X::X(int), I = 2
x2 = 2
X::~X(void), I = 2
X::~X(void), I = 1
vi main.cc
diff main.ccold main.cc 18a19
explicit
25d25
< private:
g++ -Wall -ansi -pedantic -o main main.cc
main.cc: In function `int main(int, char**)':
main.cc:34: error: conversion from `int' \
to non-scalar type `X' requested