Re: What C++0x features are relevant to you?
On Feb 23, 3:00 pm, SG <s.gesem...@gmail.com> wrote:
On 23 Feb., 03:51, "Hak...@gmail.com" <hak...@gmail.com> wrote:
I have, however, seen that adding both initializer lists and rvalue
reference-support to the same class can greatly help my code, whether
or not one should consider that a synergy.
ex:
Vector<int,3> a = {1,2,3}, b = {2,3,4}, c = {3.4.5}; // I used to have
to do so much more typing.
Vector<int,3> d = a + b + c; // Rvalue refs should have this optimized.
It depends on how you implement your Vector<> class template. It seems
your vector's dimension is part of its type which suggests that you
intend to store an array directly in the vector object (as member).
Correct.
But rvalue references would not help you in such cases (unless the
array's elements can be moved much faster than copied). Rvalue
references are great in case "logical members" are heap-allocated and
only referenced through a pointer member (see std::vector, for
example).
The way i use rvalue references is in the operator+ function:
// Pseudo code
template< T1, T2, S >
Vector<T2,S>&& operator+( const Vector<T1,S>& a, Vector<T2,S>&& b )
{
b += a;
return std::move(b);
}
Rather than creating a vector with a+b, then another with (a+b)+c, it
should create a temporary with a+b, then add c onto (a+b), then copy
that into d. Assuming the compiler doesn't optimize that line to use d
as the temporary (a+b). Though, to be honest, i haven't tested this
yet by looking into the binary...
But I'm not sure about the usefulness of the std::initializer_list
type. I mean, yes, it'll further simplify toy/example programs like
map<string,string> phonebook = {
{"William Tanner", "555-8531"},
{"Ghostbusters","555-2368"}
};
but I have a hard time imagining something like this in real code
where data comes from user inputs, files, or other sources.
With the exception of my Vector class, i never find this incredibly
useful in real code, but when i'm doing testing, making a vector of
arguments and another of expected results, i find it very useful.
I'm just doing a little thinking out loud here but maybe an
inheritance relationship is not such a bad idea:
template<class T>
class initializer_list
{
// insert implementation here
public:
T* begin() const;
T* end() const;
};
template<class T, size_t N>
class fixed_size_list : public initializer_list<T>
{
};
template<class T, size_t Dim>
class myvector
{
T coefficients[Dim];
public:
explicit myvector(std::fixed_size_list<T,Dim> initlist);
etc.
};
Just a thought. Alternativly you could try to use variadic templates
and constrain your constructor template via SFINAE a la
That fixed-size list is EXACTLY what i wish was supported. And,
really, ctors taking initializer lists could just template the size as
well and no functionality would be lost... except for how the compiler
would be generating a new version of the ctor for each different size.
#define REQUIRES(...) \ // for an unnamed template parameter
class=typename std::enable_if<(__VAR_ARGS__)>::type
template<bool... B> struct and_ : std::true_type {};
template<bool... B> struct and_<true,B...> : and_<B...> {};
template<bool... B> struct and_<false,B...> : std::false_type {};
template<class T, size_t Dim>
class myvector
{
T coefficients[Dim];
public:
template<typename... U,
REQUIRES( sizeof...(U)==Dim &&
and_<std::is_convertible<U,T>::value...>::value )
explicit myvector(U const&... args);
etc.
};
I had assumed variadic templates would suffer the same problem and be
harder to work with; harder, yes, but this was just my own ignorance.
Thanks!
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]