🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

5 total messages Started by Dirk Herrmann Mon, 26 Feb 1996 17:24
Q: default constructor for fundamental types?
#3503
Author: Dirk Herrmann
Date: Mon, 26 Feb 1996 17:24
31 lines
1522 bytes
Sorry if this has been discussed before.

When using templates, it appears that there should be
default constructors for the fundamental data types.

Example:

template <class T>
X {
  public:
    X() : t() {};
    X(const T& tt) : t(tt) {};

  private:
    T t;
}

As far as i know, the default constructor of
e.g. X<int> would not compile since there is no default
constructor for an int value.
It seems to be a pity that this would not be allowed.

--
Dirk Herrmann
---
[ To submit articles: Try just posting with your newsreader.  If that fails,
                      use mailto:std-c++@ncar.ucar.edu
  FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu 
]
Re: Q: default constructor for fundamental types?
#3514
Author: Harlan Messinger
Date: Mon, 26 Feb 1996 20:13
69 lines
2576 bytes
Dirk Herrmann (dirk@sallust.ida.ing.tu-bs.de) wrote:
: Sorry if this has been discussed before.
: 
: When using templates, it appears that there should be
: default constructors for the fundamental data types.

Essentially, there is.

: 
: Example:
: 
: template <class T>
: X {
:   public:
:     X() : t() {};
:     X(const T& tt) : t(tt) {};
: 
:   private:
:     T t;
: }
: 
: As far as i know, the default constructor of
: e.g. X<int> would not compile since there is no default
: constructor for an int value.
: It seems to be a pity that this would not be allowed.

But it appears that it is allowed. It works fine in Visual C++ 2.2. 
There's are just two things: you left out the word "class" between "template 
<class T>" and the word "X"; and you need a semicolon after the closing 
right brace of the class definition.

The following produces output of "8" on the first line and gobbledygook 
on the second line, since the effect of the "default constructor" of the 
integer member function is to do nothing.

If your X class were only meant for numeric types, you could have the 
default constructor of X call the constructor for t as t(0) instead of 
t() if you always wanted default initialization to 0.

#include <iostream.h>

template <class T>
 X {
  public:
    X() : t() {};
    X(const T& tt) : t(tt) {};
	 void output() { cout << t << "\n"; }
  private:
 	T t;
};

int main() 
{
	X<int> i(8);
	X<int> j;
	i.output();
	j.output();
	int x;
	cin >> x; // Pause for input: this is a QuickWin program.
	return 0;
}


[ To submit articles: Try just posting with your newsreader.
		      If that fails, use mailto:std-c++@ncar.ucar.edu
  FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]
Re: Q: default constructor for fundamental types?
#3519
Author: James Kanze US/ESC 60/3/141 #407...
Date: Tue, 27 Feb 1996 16:01
42 lines
2314 bytes
In article <4gsvhn$csi@clarknet.clark.net> gusty@clark.net (Harlan
Messinger) writes:

|> Dirk Herrmann (dirk@sallust.ida.ing.tu-bs.de) wrote:
|> : Sorry if this has been discussed before.
|> : 
|> : When using templates, it appears that there should be
|> : default constructors for the fundamental data types.

|> Essentially, there is.

    [Example deleted...]

|> But it appears that it is allowed. It works fine in Visual C++ 2.2. 
    [...]

|> The following produces output of "8" on the first line and gobbledygook 
|> on the second line, since the effect of the "default constructor" of the 
|> integer member function is to do nothing.

This is wrong, but since it is a recent change, it is probable that
the compiler simply hasn't implemented it yet.

Expressions of the form `int()' have always been allowed.  A (fairly)
recent change to the draft standard states that they have the same
value as default static initialization (in this case, 0).  The earlier
rules had the initailization undefined.

--
James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils, études et réalisations en logiciel orienté objet --
                -- A la recherche d'une activité dans une region francophone



[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]
Re: Q: default constructor for fundamental types?
#3530
Author: Jamshid Afshar
Date: Wed, 28 Feb 1996 01:14
29 lines
1565 bytes
In article <9602271131.AA18225@lts.sel.alcatel.de>,
James Kanze US/ESC 60/3/141 #40763  <kanze@lts.sel.alcatel.de> wrote:
>Expressions of the form `int()' have always been allowed.  A (fairly)
>recent change to the draft standard states that they have the same
>value as default static initialization (in this case, 0).  The earlier
>rules had the initailization undefined.

Is the following legal?

	class Foo {
	   int x;
	public:
	   Foo()
	     : x()   // legal?
	   {}
	};

and if so is it different than just:

	Foo::Foo() {}

--Jam
---
[ To submit articles: try just posting with your news-reader.
                      If that fails, use mailto:std-c++@ncar.ucar.edu
  FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu.
]
Re: Q: default constructor for fundamental types?
#3532
Author: Fergus Henderson
Date: Wed, 28 Feb 1996 07:25
51 lines
2217 bytes
jamshid@io.com (Jamshid Afshar) writes:

>James Kanze <kanze@lts.sel.alcatel.de> wrote:
>>Expressions of the form `int()' have always been allowed.  A (fairly)
>>recent change to the draft standard states that they have the same
>>value as default static initialization (in this case, 0).  The earlier
>>rules had the initailization undefined.
>
>Is the following legal?
>
>	class Foo {
>	   int x;
>	public:
>	   Foo()
>	     : x()   // legal?
>	   {}
>	};

Yes.

>and if so is it different than just:
>
>	Foo::Foo() {}

Yes, they are different.  The former is equivalent to

	Foo::Foo() : x(0) {}

This can be deduced from the following sections of the working paper:

	[class.base.init] 12.6.2/3
		"if the *expression-list* of the *mem-initializer* is
		omitted, the base class or member subobject is
		default-initialized (see [dcl.init])"

	[dcl.init] 8.5/5
		"To default-initialize an object of type T means:
		--if T is a non-POD class type ...;
		--if T is an array type ...;
		--otherwise, the storage for the object is zero-initialized."

--
Fergus Henderson             	WWW: http://www.cs.mu.oz.au/~fjh
fjh@cs.mu.oz.au              	PGP: finger fjh@128.250.37.3
---
[ To submit articles: try just posting with your news-reader.
                      If that fails, use mailto:std-c++@ncar.ucar.edu
  FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu.
]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads