Re: default contructing an object of unknown type T
Alf P. Steinbach /Usenet <alf.p.steinbach+usenet@gmail.com>, on
30/07/2010 19:12:31, wrote:
* Francesco S. Carta, on 30.07.2010 18:55:
Keith H Duggar <duggar@alum.mit.edu>, on 30/07/2010 09:06:01, wrote:
On Jul 30, 10:49 am, "Balog Pal"<p...@lib.hu> wrote:
"Keith H Duggar"<dug...@alum.mit.edu>
I guess what I really want is 1) and 2), after detecting whether T is
fundamental or not.
T const& t = T() ;
I vaguely recall that even that requires access to the copy ctor, and
the
compiler is allowed to use it too, though most do not. (Even more
vaguely I
recall an accident with MSVC version around 5.0 where copy was
actually used
before binding the reference.)
Yeah, you are right. How odd.
Could someone point out some reference to this requirement?
It is a requirement in C++98. There the compiler is free to go trough
any dance of temporaries created from temporaries. It is not a
requirement in C++0x.
If it is a requirement and not just an allowance, then MinGW 4.4.0 has a
bug, because it compiles the code below without any warning in -pedantic
mode. I don't know about -ansi mode because it seems to be broken (it
gives weird errors about wide chars).
Besides, does the following:
const T& t = *(new T);
suffer from the same issue about temporaries and copy ctor required?
//-------
#include <iostream>
#include <string>
using namespace std;
class NoCopyNoAssign {
public:
NoCopyNoAssign(string s = "[not specified]") : s(s) {}
friend ostream& operator<<(ostream& os, const NoCopyNoAssign& obj) {
os << obj.s;
return os;
}
private:
NoCopyNoAssign(const NoCopyNoAssign&);
NoCopyNoAssign& operator=(const NoCopyNoAssign&);
string s;
};
template<class T> void f(const T& passed) {
const T& local = T();
cout << "passed == " << passed << endl;
cout << "local == " << local << endl;
}
int main() {
NoCopyNoAssign ncna("initialized");
f(ncna);
int i = 42;
f(i);
}
//-------
--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com