|
Boost : |
From: Andrew Koenig (ark_at_[hidden])
Date: 2002-08-20 12:57:52
dave> I have to admit, I don't know the rules, but they seem terribly nonuniform
dave> to me. The specific problem I'm trying to solve is the implementation of
dave> boost/python/implicitly_convertible.hpp:
dave> template <class Source, class Target>
dave> void implicitly_convertible();
dave> Requires: The expression Target(s), where s is of type Source, is
dave> valid.
dave> Internally, this function needs to do a placement-new of the target type.
dave> Naturally, I thought that internally writing:
dave> new (storage) Target(src)
dave> would work. However, that doesn't work for all types. For example, if src
dave> is an int and Target is an enum type, I can write:
dave> Target x = Target(src);
dave> but not:
dave> Target* p = new Target(src);
dave> That's just wierd.
Writing `new Target(src)' should work if you can write
Target x(src);
which you can't if Target is an enum type and src is an int value.
dave> I guess I could use a named variable instead:
dave> template <class Src, Target> int g(Src const& x)
dave> {
dave> Target t = x;
dave> f(t);
dave> return 0;
dave> }
dave> Is that the best answer?
What about
Target t(x);
dave> Finally, there's the issue of default construction. If I want to
dave> generically declare a default-constructed object of type T, I
dave> can't write:
dave> T x;
dave> since builtins (and I presume, enums) won't be initialized. I
dave> can't write:
dave> T x();
dave> since that just declares a function. What can/should I write?
You can write
static T x;
as long as you're not planning to clobber the value of x.
For that matter, what about this?
struct Temp {
T x;
Temp(): x() { }
}
Temp t;
and then use t.x
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk