I have a couple of classes A, B, and F, all having different constructor signatures, like these:
class A {
public:
A() {}
};
class B {
double x_, y_;
public:
B(double x, double y) : x_(x), y_(y) {}
};
class F {
vector<unsigned> factors_;
public:
F(std::initializer_list<unsigned> factors) : factors_(factors) {}
};
The objects of these classes need to be registered somewhere, so the construction should go through a factory, like this:
template<class R, typename... Args>
inline R & factory(Args &&... args)
{
R * rp = new R(std::forward<Args>(args)...);
/* do fancy stuff with rp... */
return *rp;
}
This works fine for classes A and B, but not for F, because typename... wants to take one or more types:
A & a = factory<A>(); // okay
B & b = factory<B>(0.707107, 0.707107); // okay
F & f = factory<F>({2, 3, 5, 7, 11}); // error: no matching function for call to ‘F::F()’ in factory
Question: Is there any way to make it work with the general factory<R>(args...) syntax?
I tried a full specialization of the factory function for <F, std::initializer_list>, but either messed up the syntax or, when it compiled, wasn't picked up as factory by the compiler.
Any other ideas?
To answer the question: I can't see a simple way of reusing your factory method which takes an initializer_list.
I would personally add a constructor in class F taking a std::vector
F(const std::vector<unsigned>& factors) : factors_(factors) {}
and rewrite a factory method :
template<class R>
inline R & factory(const std::vector<unsigned>& factors)
{
R *rp = new R(factors);
/* do fancy stuff with rp... */
return *rp;
}
Now it compiles fine.
Live code
If you want to avoid code duplication you should consider writing your /* do fancy stuff with rp... */ in another function.
Edit
I don't know what you want to do, but why don't you simply use here :
F f2{2, 3, 5, 7, 11};
You don't need a factory if you just want to populate your class F vector with some values.
It might not be your preferred syntax, but you can force the factory method to pass an initializer list:
F & f = factory<F>(std::initializer_list<unsigned>{2u, 3u, 5u, 7u, 11u});
I explicitly made the literals unsigned here - although they could be coerced by the std::initializer_list<unsigned>, that isn't the case with the following version.
I was able to get it working with an overload of factory():
template<class R>
inline R& do_fancy_stuff_with(R *r)
{
/* do fancy stuff here... */
return *r;
}
template<class R, typename... Args>
inline R & factory(Args &&... args)
{
R * rp = new R(std::forward<Args>(args)...);
return do_fancy_stuff_with(rp);
}
template<class R, typename T>
inline R & factory(std::initializer_list<T> args)
{
R * rp = new R(std::move(args));
return do_fancy_stuff_with(rp);
}
A & a = factory<A>();
B & b = factory<B>(0.707107, 0.707107);
F & f = factory<F>({2u, 3u, 5u, 7u, 11u});
I'm surprised that variadic templates don't play more nicely with initializer lists.
Related
I like the new std::move but afraid that it reduces my program maintainability.
To my knowledge, if I create move constructor or move assignment operator=(), I have to write it from scratch. That is where the problem begins.
Code version 1
Here is a tiny class:-
class B{
M shouldBeMove; //if it is copied, it is still correct (but prefer move)
C shouldBeCopy; //can be copied or moved, both are equal and ok
//wow, I don't even have to write this line for operator=():-
// this->shouldBeCopy = that.shouldBeCopy
}
B b1;
B b2=b1;
Currently, B b2=b1 will copy both M and C. It is ok.
Code version 2
Now I want to use the power of std::move :-
class B{
M shouldBeMove; //now, the program is refactored that it must be moved
// M now has "M& operator=(M&& that)"
C shouldBeCopy;
B& operator=(B&& that){
this->shouldBeMove=std::move(that.shouldBeMove);
this->shouldBeCopy=that.shouldBeCopy; //<-- a bit tedious (1#)
// ... imagine that there are 10 variables to be copied ...
}
}
B b1;
B b2=std::move(b1);
It is still ok, but a bit tedious. (1#)
Code version 3
Then one month in the future, I may want to add a new field e.g. C shouldBeCopy2 to B, I also have to add a line into operator= :-
B& operator=(B&& that){
this->shouldBeMove=std::move(that.shouldBeMove);
this->shouldBeCopy=that.shouldBeCopy;
this->shouldBeCopy2=that.shouldBeCopy2; //<--- new line
}
I think I am a type that may forget to add that line. (2#)
Question:
1#. How to make it not tedious?
2#. How to foolproof my mistake?
You should follow rule of zero and let compiler generate the constructors and assign operators for you.
But when you need to implement a moveable type, make sure you implement both move assignment operator (T& operator=(T&&)) and move constructor (T(T&&)). Please follow rule of five and ensure the class have proper copy constructor/move constructor/copy assignment operator/move assignment operator/destructor
https://ideone.com/UVZNOM
#include <iostream>
using namespace std;
class M{
public: int database=0;
M& operator=(M&& other){
this->database=other.database;
other.database=0;
return *this;
}
M(M &&other) {
*this = std::move(other);
}
M (M& m)=default;
M ()=default;
~M() { /* free db */ }
};
class B{ // As rule of zero, you don't need to implement constructors and assignment operators
public: M shouldMove;
};
int main() {
B b;
b.shouldMove.database=5;
B b2=std::move(b);
std::cout<< b.shouldMove.database <<std::endl;
std::cout<< b2.shouldMove.database <<std::endl;
return 0;
}
In the code below I show union-like class S which contains two non-related structs B and C. I show how to instantiate the non-POD std::string and delete it again and then switch S to S::CC and set the num int.
#include <vector>
#include <string>
#include <iostream>
#include <memory>
struct B
{
B() {}
~B() {}
std::string str;
void Func1() {}
};
struct C
{
C() {}
~C() {}
int num;
void Func2() {}
};
struct S
{
S() { tag = CC; }
S( const S& s )
{
switch( s.tag )
{
case BB:
new ( &b.str ) std::string;
b.str = s.b.str;
break;
case CC:
c.num = s.c.num;
default:
break;
}
}
~S()
{
switch( tag )
{
case BB:
b.str.~basic_string< char >();
break;
case CC:
c.num = 0;
break;
default:
break;
}
}
enum { BB, CC } tag;
union
{
B b;
C c;
};
};
struct H
{
H( std::initializer_list< S > initializerList ) : initListVect( initializerList ) {}
std::vector< S > initListVect;
};
int main()
{
S s;
s.tag = S::BB;
new ( &s.b.str ) std::string; // docs say use new placement to create memory
s.b.str = "bbb";
s.b.str.~basic_string< char >(); // string usage in B ok
s.tag = S::CC;
s.c.num = 333; // int usage in C ok
H h { }; // what should the init list be if I wanted 3 list elements S::BB, S::CC, S::BB?
return 0;
}
My goal, however, is to use S in an std::initializer_list. I don’t know what the format should be for initializeing h. What should the arguments be if I wanted to initialize h with these S::BB, S::CC, S::BB?
My compiler is VS2015.
Edit:
This post’s history: my posting comes from a need for a definitive answer to the question of storing compile-time-deduceable heterogeneous objects in an std::initializer_list. This question has been asked many times before and there have been many attempts at answers (see Heterogeneous containers in C++). The most simplistic answer is to use polymorphism, but this ignores the power of being able to define a type at compile time (templates). Besides, heterogeneous, non-related objects grouped together polymorphically means a lot of derived data members are useless, which sows usage and maintenance confusion downstream. Other advice given was to use boost::any or boost::variant, but this has the same weakness as polymorphism and reduces message declaration clarity. Another attempt at container object heterogeneity was the use of std::tuple, but although an initializer_list can certainly contain tuples, this approach too ignores compile-time type resolution. I even found a paper written in 1999 called Heterogeneous, Nested STL Containers in C++ which uses template template arguments to solve the heterogeneity problem. After all this, I settled on class-like unions which led to my posting here. Class-like unions for non-related/heterogeneous container objects has perfect message declaration clarity, no object size ambiguity, and is compile time template-able, and it leads to excellent downstream maintenance scenarios.
Edit2: (5 weeks later) Here is what has happened. 1) I implemented a full class-like union solution given the advice in this posting. The result was tedious and unwieldy with ‘tag’ being used to identify which sub-method to call for each new functionality. Low grade regarding code maintenance. 2) c++17 has accepted std::variant. Since that is currently not yet implemented in VS2015 Update 2, I set about using boost::variant. See What is the right c++ variant syntax for calling a member function set to a particular variant? which uses the Visitor pattern to allow access to initialized variant members and member functions. This eliminates the ‘tag’ switches and variant ‘get’ calls. Bottom line: I dropped my class-like union and adopted variant for creating maintainable code that uses initializer_list to store variant member functionality all being initializable at compile time (read: highly maintainable).
Alright, I'm feeling generous and I've made custom unions myself so he're some stuff that'll get you set up. I've rewritten your S structure to be more compliant and usable. (I've made changes marked by comments)
struct S
{
S() : tag(CC) // initializer
{
new (&c) C; // make C object
}
S(int num) : tag(CC) // added integer constructor
{
new (&c) C;
c.num = num;
}
S(const std::string& str) : tag(BB) // added string constructor
{
new (&b) B;
b.str = str;
}
S( const S& s ) : tag(s.tag)
{
if (tag == CC)
{
new (&c) C; // construct c
c.num = s.c.num;
}
else if (tag == BB)
{
new (&b) B; // construct b, not b.str
b.str = s.b.str;
}
}
S& operator= (const S& s) // added assignment operator
{
if (tag == s.tag) // just copy b or c
{
if (tag == CC)
c = s.c;
else
b = s.b;
}
else // reconstruct b or c
{
if (tag == CC)
{
c.~C(); // destroy c
new (&b) B; // construct b
b.str = s.b.str;
}
else
{
b.~B(); // destroy b
new (&c) C; // construct c
c.num = s.c.num;
}
tag = s.tag;
}
return *this;
}
~S()
{
if (tag == CC)
{
c.~C(); // destroy c
}
else if (tag == BB)
{
b.~B(); // destroy b, not b.str
}
}
enum { BB, CC } tag;
union
{
B b;
C c;
};
};
One of the things that you were doing improperly was skipping the construction and destruction of B and C and going straight for the internal variables. You should always create and destroy types properly even when they may be trivial. While this may work out, not initializing these objects properly is only asking for trouble (It also makes it easier should you change B or C in the future).
To make using the class easier, I added in the proper constructors for std::string and int as well as an assignment operator. Because now that we can construct the objects how we want, your main() could look like this:
int main()
{
S s; // default S
s = std::string("bbb"); // set to string
s = 333; // set to number
// use initialization list
H h { std::string("bb"), 33, std::string("bb") };
return 0;
}
I encourage you to modify B and C to use constructors to build their internals rather than relying on S.
I got a class that looks like this
class Rational{
public:
Rational(int p = 0, int q = 1) : p(p), q(q){};
private:
int p;
int q;
};
My question is about the initialization syntax where member variables and constructor parameters has identical names.
I now know it is legal to do so, but my question is:
If I want do have "clean", easy to grasp code I wonder if I can do as one normally would do in java:
//Legal Java Code
this.q = q;
this.p = p;
//Is any of these legal C++ code (if so, which one)?
this.q(q);
this.p(p);
//or
this->q(q);
this->p(p);
Even though I haven't tested it, and I can test it, I still want to know the C++ conventions of doing this.
In C++, you have to say:
this -> q = q;
this -> p = p;
or equivalently
(*this).q = q;
(*this).p = p;
But I think the member initializer list syntax
Rational(int p = 0, int q = 1) : p(p), q(q){}
is cleaner (note the lack of a semicolon!) Why don't you like it?
Sorry - I'm sure I'm just not finding any answers because I don't know the nomenclature!
class Foo
{
public:
Foo() { } // default ctor
explicit Foo(int a) : _a(a) { } // semi-explicit - but what is _b's value??
protected:
int _a = 9; // HOW DOES THIS RELATE TO EACH OF THE CTORS?!
int _b = 3; // HOW DOES THIS RELATE TO EACH OF THE CTORS?!
};
By explicitly specifying a default ctor w/o specifying _a or _b, do the declared assignments happen (_a = 9, _b = 3) or do those only happen if I don't create a default ctor (or I declare as Foo() = default;)?
C++11 [class.base.init]/8:
In a non-delegating constructor, if a given non-static data member or base class is not designated by a mem-initializer-id (including the case where there is no mem-initializer-list because the constructor has no ctor-initializer) and the entity is not a virtual base class of an abstract class (10.4), then
— if the entity is a non-static data member that has a brace-or-equal-initializer, the entity is initialized as specified in 8.5;
— ...
So, in your case, a default-initialized Foo has _a == 9 and _b == 3 since neither _a or _b appear in the (absent) mem-initializer-list. Foo(7) will have _a == 7 and _b == 3, since _b does not appear in the mem-initializer-list.
I have looked over this thread which talks about using this method for comparison:
struct thing
{
int a;
char b;
bool operator<(const thing &o) const
{
return a < o.a;
}
};
priority_queue<thing> pq;
On the other hand other uses method such as this:
struct Time {
int h;
int m;
int s;
};
class CompareTime {
public:
bool operator()(Time& t1, Time& t2) // Returns true if t1 is earlier than t2
{
if (t1.h < t2.h) return true;
if (t1.h == t2.h && t1.m < t2.m) return true;
if (t1.h == t2.h && t1.m == t2.m && t1.s < t2.s) return true;
return false;
}
}
priority_queue<Time, vector<Time>, CompareTime> pq;
While I logic myself with the first method, I don't quit understand the second method. Mostly because of the syntax. I am not quit sure what the overloading operator operator() means. What is that operator overloading?
Also, from cplusplus on priority_queue, I don't quite understand the following, mainly the second parameter.
template < class T, class Container = vector<T>,
class Compare = less<typename Container::value_type> > class priority_queue;
In another word, I don't understand the second method and its calling convention.
Also, what's the difference and which method is preferred?
I am not quit sure what the overloading operator operator() means.
What is that operator overloading?
What we have here is an overloading of the function call operator (see SO question) , this means that client code can 'treat' the CompareTime class instances as compare functions :
CompareTime ct;
if ( ct(t1, t2) )
{
...
}
I don't quite understand the following, mainly the second parameter.
The cplusplus reference summarizes quite well , the template parameters :
0 arg - The type of the objects within the queue.
1 arg - The underlying container/data structure for the queue, by
default its the std vector
2 arg - Operation on priority queue relies on some precedence
comparison, i.e. which item in the queue should be 'before' other item (see also Wikipedia , so this arg accepts to have compare object (functor) which mean instances of plain class which overload the () operator , the default is the std less functor which is simply a wrapper above the '<' semantics (boolean 2 valued function object).
// TEMPLATE STRUCT less
template<class _Ty>
struct less : public binary_function<_Ty, _Ty, bool>
{
// functor for operator<
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{
// apply operator< to operands
return (_Left < _Right);
}
};