Topic: Built-in universal method of default-initialisation


Author: fgothamNO@SPAM.com (Frederick Gotham)
Date: Mon, 31 Jul 2006 05:10:27 GMT
Raw View
Frederick Gotham posted:

> At present, the language doesn't provide a universal method of default
> initialisation for all types.


I came up with the following today. How does it look? It should be
perfectly efficient, right?

#include <sstream>
#include <string>

struct MyPOD {
    int *p;
    char a;
    long i;
    double d;
};

struct MyUnion {
    MyPOD a;
    int b;
    float c;
};

template<class T>
void Func()
{
    T arr[1] = {};  /* Here the trickery lies */
    T &obj = *arr;

    (void)obj;

    /* Now we can use "obj" */
}

int main()
{
    Func<std::ostringstream>();
    Func<std::string>();
    Func<MyPOD>();
    Func<MyUnion>();
    Func<int>();
    Func<long>();
    Func<void*>();
    Func<double>();
}

--

Frederick Gotham

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Christopher Conrade Zseleghovski <kkz@duch.mimuw.edu.pl>
Date: Sat, 22 Jul 2006 15:59:25 CST
Raw View
Frederick Gotham <fgothamNO@spam.com> wrote:
> "Andrew Koenig" posted:
>
>> "Frederick Gotham" <fgothamNO@SPAM.com> wrote in message
>> news:5yutg.11418$j7.315735@news.indigo.ie...
>>
>>> There's a well-known universal method for default-initialising an
>>> object regardless of its type, but I was thinking it would be handy if
>>> the language provided it built-in.
>>
>> Can you show us an example of where you would use such a feature if you
>> had it?
>
>
> Here's a template function which would facilitate the idea of having a
> "null reference":
>
>
> template<class T>
> inline T const &NullObj()
> {
>    DefInit<T> const static obj;
>
>    return obj.obj;
> }
>
>
> (And here's the definition of "DefInit":)
>
> template<class T>
> struct DefInit {
>
>    T obj;
>
>    DefInit() : obj() {}
> };
>
>
> It would be better if the template could be written simply as:
>
>
> template<class T>
> inline T const &NullObj()
> {
>    T const static obj default;

// I have no problem with using the initializer

static T const obj((T()));

// Chris

>
>    return obj;
> }
>
>

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: fgothamNO@SPAM.com (Frederick Gotham)
Date: Sun, 23 Jul 2006 17:45:46 GMT
Raw View
Christopher Conrade Zseleghovski posted:

> // I have no problem with using the initializer
>
> static T const obj((T()));
>
> // Chris


Firstly, I picked a bad example, because static data always gets default
initialised. (Don't know how I managed that!)

Let's stick with automatic variables.

Your own method is inadequate as a "universal method of default-
initialising an object", as demonstrated by the following snippet:

#include <sstream>

int main()
{
    using std::ostringstream;

    ostringstream const obj( (ostringstream()) );
}


G++: std::ios_base::ios_base(const std::ios_base&)' is private


--

Frederick Gotham

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: fgothamNO@SPAM.com (Frederick Gotham)
Date: Tue, 18 Jul 2006 15:26:18 GMT
Raw View
"Andrew Koenig" posted:

> "Frederick Gotham" <fgothamNO@SPAM.com> wrote in message
> news:5yutg.11418$j7.315735@news.indigo.ie...
>
>> There's a well-known universal method for default-initialising an
>> object regardless of its type, but I was thinking it would be handy if
>> the language provided it built-in.
>
> Can you show us an example of where you would use such a feature if you
> had it?


Here's a template function which would facilitate the idea of having a
"null reference":


template<class T>
inline T const &NullObj()
{
    DefInit<T> const static obj;

    return obj.obj;
}


(And here's the definition of "DefInit":)

template<class T>
struct DefInit {

    T obj;

    DefInit() : obj() {}
};


It would be better if the template could be written simply as:


template<class T>
inline T const &NullObj()
{
    T const static obj default;

    return obj;
}


--

Frederick Gotham

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: fgothamNO@SPAM.com (Frederick Gotham)
Date: Thu, 13 Jul 2006 21:47:22 GMT
Raw View
At present, the language doesn't provide a universal method of default
initialisation for all types. The closest thing we have is:

    Type obj = Type();


but that's inadequate as it requires a public copy-constructor.

There's a well-known universal method for default-initialising an object
regardless of its type, but I was thinking it would be handy if the
language provided it built-in. Something like:


template<class T>
void Func()
{
    T obj default;
}


For aggregates, this would be equivalent to:

    T obj = {};

For classes with a constructor, it would be equivalent to:

    T obj;

For intrinsics, it would be equivalent to:

    T obj = 0;


It wouldn't be "keyword abuse" either, because "default" is quite becoming
in this context.


--

Frederick Gotham

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: ark@acm.org ("Andrew Koenig")
Date: Thu, 13 Jul 2006 23:20:39 GMT
Raw View
"Frederick Gotham" <fgothamNO@SPAM.com> wrote in message
news:5yutg.11418$j7.315735@news.indigo.ie...

> There's a well-known universal method for default-initialising an object
> regardless of its type, but I was thinking it would be handy if the
> language provided it built-in.

Can you show us an example of where you would use such a feature if you had
it?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]