I actually had PIMPL in mind, and variadic constructors seemed like a nice solution until they killed the copy constructors and various other features depending on how they're used (initializer lists).
Originally Posted by
CornedBee
I would suggest SFINAEing out the offending instantiation of the variadic constructor, but that actually seems not possible without seriously inconveniencing the client of the class.
Yeah, and the entire purpose of the variadic constructor is convenience of the class clients unfortunately.
Originally Posted by
CornedBee
the problem is that AFAIK GCC doesn't support constructor forwarding yet, so you actually have to duplicate the implementation (at least the init lists, the other stuff can be combined).
That looks like the case.
Originally Posted by
CornedBee
Alternatively, make it so your variadic constructor does the right thing for a single argument of its class type. But that may not be a design option.
Yup, not possible in this case unfortunately :[
Originally Posted by
CornedBee
Hmm ... you might want to write to comp.std.c++ about this problem.
I'm too noob, I'd get eaten alive.
Everything works non-const and explicit variadic constructor except certain things like initializer lists.
Thanks much CornedBee, appreciate the responses :]
It's is probably too much work.. but for instance this is kind of what I was thinking. What do you think?
main.h
Code:
#ifndef MAIN_H
#define MAIN_H
#include <iostream>
#include <boost/shared_ptr.hpp>
class klass
{
class impl;
public:
klass();
klass(klass& p);
klass(klass const& p);
template<typename ...argument_types> explicit klass(argument_types&& ...argument_list);
void do_something();
void print_multiple();
private:
boost::shared_ptr<impl> operator->();
boost::shared_ptr<impl> data;
};
#endif
main.cpp
Code:
#include "main.h"
class klass::impl
{
public:
impl() { }
impl(int x, int y) : x(x), y(y) { }
private:
int x;
int y;
friend class klass;
};
klass::klass() : data(new klass::impl) { }
template<typename ...argument_types>
klass::klass(argument_types&& ...argument_list) : data(new klass::impl(argument_list...)) { }
klass::klass(klass& p) : data(p.data) { }
klass::klass(klass const& p) : data(p.data) { }
boost::shared_ptr<klass::impl> klass::operator->() { return this->data; }
void klass::do_something()
{
auto self = *this;
self->x = 10;
self->y = 10;
}
void klass::print_multiple()
{
auto self = *this;
std::cout << self->x * self->y << std::endl;
}
int main()
{
klass a(5, 5); // klass a = {5, 5}; doesn't work
a.print_multiple(); // 25
a.do_something(); // change to 10 x 10
a.print_multiple(); // 100
klass b(a);
b.print_multiple(); // 100
}