 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
andrew_nuss@yahoo.com Guest
|
Posted: Fri May 19, 2006 10:21 am Post subject: problem w/ Conversion template on pg 36 of Alexandrescu |
|
|
Hi All,
I am trying to do an optimization with an Array<T> class of my own
which allocates the
array on a heap in the constructor. Some of my T's are ints, some are
pointers, and some
are classes.
Of the classes, some classes have ctors/dtors and some do not. For
those that do not
have ctors/dtors, I do not want to iterate the heap buffer and invoke
placement new constructors
at all but just leave the initial array garbage as the elem T
constructor would leave it as garbage
anyway.
Therefore, I have created an empty struct:
struct CtorDtorLess {};
and any struct T which has no ctor/dtor extends CtorDtorLess. Then I
have
used specialization in an array policy that specializes only structs
which do not
widen to CtorDtorLess. Does this sound reasonable?
The problem is that the Conversion template on pg. 36 of Alexandrescu:
template <class T, class U>
class Conversion {
typedef char Small;
class Big { char dummy[2]; };
static Small Test(U);
static Big Test(...);
static T MakeT();
public:
enum { exists = sizeof(Test(MakeT())) == sizeof(Small) };
};
gives my Intel C++ Compiler for Linux a headache. In fact, I get a
"warning 1595:
non-POD class type passed through ellipsis". Does anyone know what
this warning
means? Has anyone had problems with the pg 36 Conversion template from
"Modern C++ Design"?
Andy
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Tom Widmer Guest
|
Posted: Sat May 20, 2006 9:21 pm Post subject: Re: problem w/ Conversion template on pg 36 of Alexandrescu |
|
|
andrew_nuss (AT) yahoo (DOT) com wrote:
| Quote: | Hi All,
I am trying to do an optimization with an Array<T> class of my own
which allocates the
array on a heap in the constructor. Some of my T's are ints, some are
pointers, and some
are classes.
Of the classes, some classes have ctors/dtors and some do not. For
those that do not
have ctors/dtors, I do not want to iterate the heap buffer and invoke
placement new constructors
at all but just leave the initial array garbage as the elem T
constructor would leave it as garbage
anyway.
Therefore, I have created an empty struct:
struct CtorDtorLess {};
and any struct T which has no ctor/dtor extends CtorDtorLess. Then I
have
used specialization in an array policy that specializes only structs
which do not
widen to CtorDtorLess. Does this sound reasonable?
|
Well, it's a very invasive method of doing it (what if you can't change
a T to extend CtorDtorLess for some reason). Usually this is handled by
traits (or even automatic type_traits). In an ideal world, you can use:
std::tr1::has_trivial_constructor<T>
and
std::tr1::has_trivial_destructor<T>
This currently have compiler support on a few platforms, and an
implementation is available from www.boost.org. Even if your platform
doesn't have automatic support, you can specialize the above 2 classes
for the relevant Ts yourself.
| Quote: | The problem is that the Conversion template on pg. 36 of Alexandrescu:
template <class T, class U
class Conversion {
typedef char Small;
class Big { char dummy[2]; };
static Small Test(U);
static Big Test(...);
static T MakeT();
public:
enum { exists = sizeof(Test(MakeT())) == sizeof(Small) };
};
gives my Intel C++ Compiler for Linux a headache. In fact, I get a
"warning 1595:
non-POD class type passed through ellipsis". Does anyone know what
this warning
means?
|
Well, it's undefined behaviour to pass a non-POD (like T above) to a
method taking "...". The warning is bogus though, since sizeof doesn't
evaluate its argument. Boost gets rid of such warnings by using
alternative implementation techniques for its type_traits (which are
part of a standard technical report, std::tr1).
Tom
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|