C++ get default value of class member without creating new object - c++

Let's consider definition:
struct ClassWithMember
{
int myIntMember = 10;
}
I'd like to get myIntMember's default value, but do not create another instance of the class
// IMPOSSIBLE int myInt = ClassWithMember::myIntMember;
// MUST AVOID int myInt = ClassWithMember().myIntMember;
I know workaround, but dislike it:
struct ClassWithMember
{
static const int myIntMember_DEFAULT = 10;
int myIntMember = myIntMember_DEFAULT;
}
int myInt = ClassWithMember::myIntMember_DEFAULT;
Because it needs extra line. And I cannot define inline static pointers like static const int *const INTEGER11_DEFAULT = 0x100088855;, such pointers must be defined in .cpp file, in .hpp is only declaration. Many of my classes are header-only, so create excess .cpp for this value isn't good idea.
Here is the similar question for C#

I suppose that, for you, is only another workaround but I think is a little (only a little) more practical.
If you save the default value as a static const that is returned by a static method, you can avoid the additional line in the cpp file.
The following example do the trick in a template wrapper (with default value as template parameter with a default vaule; just for fun) but the template part is just to avoid the duplication of code in the example
#include <iostream>
template <typename T, T defTpl = T{}>
struct wrapperWithDef
{
static T getDefVal ()
{ static T const def { defTpl }; return def; }
T myTMember { getDefVal() };
};
int main()
{
wrapperWithDef<int> wi;
wrapperWithDef<long, 3L> wl;
wrapperWithDef<int *> wp;
// print "0, 3, (nil)" (clang++) or "0, 3, 0" (g++)
std::cout << wi.myTMember << ", " << wl.myTMember << ", "
<< wp.myTMember << std::endl;
// print "5, (nil), 1" (clang++) or "5, 0, 1" (g++)
std::cout << wrapperWithDef<unsigned, 5U>::getDefVal() << ", "
<< wrapperWithDef<long *>::getDefVal() << ", "
<< wrapperWithDef<bool, true>::getDefVal() << std::endl;
return 0;
}

I call this solution workaround
struct ClassWithMember
{
static const int myIntMember_DEFAULT = 10;
int myIntMember = myIntMember_DEFAULT;
}
int myInt = ClassWithMember::myIntMember_DEFAULT;
For pointers it will look more complicated
//.hpp
struct ClassWithMember
{
static AnotherClass* const myMember_DEFAULT; //=X; Assignment not allowed
AnotherClass* myMember = myMember_DEFAULT;
}
//.cpp
AnotherClass* const MyNamespace::ClassWithMember::myMember_DEFAULT = pAnotherInstance;
//usage
auto *my = ClassWithMember::myMember_DEFAULT;

Related

Library to store an "equation" involving references?

So I can pass by reference, and store that reference in a structure or class, and if I make changes elsewhere and check that reference again where I stored it the changes will be there because I'm just accessing the same memory.
Is there a library that will let me do something like this:
int foo = 9;
int bar = 5;
// obviously other arithmetic would exist too, and could be combined
Equation foo_minus_bar = Subtract(foo, bar);
// output: 4
cout << foo_minus_bar << endl;
foo = 11;
// output: 6
cout << foo_minus_bar << endl;
It would also be nice if I could access the inputs (preferably as a flat array or similar, but beggars can't be choosers, and maybe even something like this:
// literal character for character output: foo - bar
cout << foo_minus_bar.formula() << endl;
I could make one myself, but I'd rather not reinvent the wheel if it exists.
The question of OP reminds me to another answer where I modeled an AST for a small example compiler with functor-like classes: The Tiny Calculator Project.
In that project the AST expression nodes have ownership of their child (expression) nodes.
I'm not sure whether I read the intention of OP correctly but, of course, it can be designed as well with expression nodes which don't have ownership of child (expression) nodes.
Thus, I made another (even shorter) example. Additionally, I overloaded operator()() (instead of a virtual solve() member function). Though, in this case, I consider it as a matter of taste.
Sample code:
#include <iostream>
struct Expr {
virtual int operator()() const = 0;
};
struct ExprConst: Expr {
const int value;
ExprConst(int value): value(value) { }
virtual int operator()() const { return value; }
};
struct ExprRef: Expr {
const int &ref;
ExprRef(const int &ref): ref(ref) { }
virtual int operator()() const { return ref; }
};
struct ExprBin: Expr {
const Expr &arg1, &arg2;
ExprBin(const Expr &arg1, const Expr &arg2):
arg1(arg1), arg2(arg2)
{ }
};
struct ExprSub: ExprBin {
ExprSub(const Expr &arg1, const Expr &arg2):
ExprBin(arg1, arg2)
{ }
virtual int operator()() const { return arg1() - arg2(); }
};
int main()
{
int foo = 9;
int bar = 5;
ExprRef exprFoo(foo), exprBar(bar);
ExprSub exprSub(exprFoo, exprBar);
std::cout << "foo - bar: " << exprSub() << '\n';
std::cout << "foo = 7; bar = 10;\n";
foo = 7; bar = 10;
std::cout << "foo - bar: " << exprSub() << '\n';
// done
return 0;
}
Output:
foo - bar: 4
foo = 7; bar = 10;
foo - bar: -3
Live Demo on coliru

map strings to class members

Say I have a class / struct like below:
struct A {
uint32_t a;
uint8_t b;
uint16_t c;
};
And I have a set of strings that associate to each member of A (could be of different integer types, but not non-integer types such as strings), e.g.
"field1" -> A::a
"field2" -> A::b
"field3" -> A::c
Assume that there is always a 1:1 mapping between the strings and members. Is there an elegant way to map each string to each member using something like std::unordered_map?
I want to be able to read and write to each field using the strings as keys, e.g.
A a {1,2,3};
mymap["field1"] = 4; // a.a = 4
mymap["field2"] = 5; // a.b = 5
auto c = mymap["field3"]; // c = a.c = 3
I'm using C++11/14 and can't use boost.
Some more information on the context of this question:
The struct A mentioned in the question are settings of the program. They are hardware configuration parameters and my software program is used to simulate hardware behaviours. These settings/configurations are script-generated structs like above. We read / write these struct members and because the amount of these settings/configurations are so many (a few thousands), it would be convenient to be able to access them by their names as well. This is how & why I want to associate each member with a string. Whether it is subscripting or a function to access the corresponding members does not really matter, but there is such a 1:1 mapping between a string (name of the setting) and the generated struct member. And as I mentioned in the question, these members are of different integer types.
You could make a proxy-class that wraps around a integer, then store this proxy class in a std::unordered_map.
#include <iostream>
#include <functional>
#include <unordered_map>
struct A {
uint32_t a;
uint8_t b;
uint16_t c;
};
struct ValueWrapper {
using value_type = uint64_t;
template <typename Obj, typename T>
ValueWrapper(Obj& obj, T Obj::*member) {
get = [&, member]() { return obj.*member; };
set = [&, member](value_type value) mutable { obj.*member = value; };
}
ValueWrapper() = default;
ValueWrapper& operator=(value_type value) {
set(value);
return *this;
}
operator value_type() {
return get();
}
std::function<value_type()> get;
std::function<void(value_type)> set;
};
std::unordered_map<std::string, ValueWrapper> make_map(A& a) {
std::unordered_map<std::string, ValueWrapper> map;
map["field1"] = ValueWrapper(a, &A::a);
map["field2"] = ValueWrapper(a, &A::b);
map["field3"] = ValueWrapper(a, &A::c);
return map;
}
int main() {
A a{1,2,3};
auto map = make_map(a);
map["field2"] = 67;
std::cout << a.a << " " << static_cast<int>(a.b) << " " << a.c << std::endl;
std::cout << map["field1"] << " " << map["field2"] << " " << map["field3"] << std::endl;
}
You do get some restrictions depending on the value_type. If you use int64_t you could wrap anything but a uint64_t safely. If you go for a uint64_t you could wrap all the unsigned integers, but not the signed ones safely.
I put the default constructor there to satisfy unordered_maps use of operator[].
template<class V>
struct pseudo_ref_t {
operator V()&& { return getter(); }
void operator=(V v)&&{
setter(std::move(v));
}
std::function<void(V)> setter;
std::function<V()> getter;
};
template<class T, class V>
struct member_t {
friend pseudo_ref_t<V> operator->*( T* t, member_t const& self ) {
return {
[&self, t](V in){ self.setter(*t, std::move(in)); },
[&self, t]()->V{ return self.getter(*t); }
};
}
friend V operator->*( T const* t, member_t const& self ) {
return self.getter(*t);
}
std::function<void(T&, V)> setter;
std::function<V(T const&)> getter;
};
template<class T, class V, class X>
member_t<T, V> make_member( X T::* mem_ptr ) {
return {
[mem_ptr](T& t, V in) {
(t.*mem_ptr) = std::move(in);
},
[mem_ptr](T const& t)->V {
return (t.*mem_ptr);
}
};
}
a member_t<A, uint32_t> can type-erase any member of A that implicitly convertible to/from a uint32_t.
It acts like a smart member pointer.
you said that it could have different integer types, but it is a problem for having an elegant solution. If you can set up on a single type, this would be simple, like the following.
#include <iostream>
#include <map>
struct A {
int a;
int b;
int c;
};
using namespace std;
int main() {
map<string, int A::*> abc = {
{"a", &A::a},
{"b", &A::b},
{"c", &A::c}
};
A aa;
aa.*abc["a"] = 1;
aa.*abc["b"] = 2;
aa.*abc["c"] = 3;
cout << "a = " << aa.a << "(" << aa.*abc["a"] << ")" << endl;
cout << "b = " << aa.b << "(" << aa.*abc["b"] << ")" << endl;
cout << "c = " << aa.c << "(" << aa.*abc["c"] << ")" << endl;
return 0;
}
The main challenge of your question is that different members have different types.
If you make the types the same, you can use a lot of tricks.
For your case, I know you don't want to use Boost or C++17, but just to show you the challenges ahead, let me give you a Boost.Hana, C++17 solution.
#define BOOST_HANA_CONFIG_ENABLE_STRING_UDL
#include <boost/hana/equal.hpp>
#include <boost/hana/string.hpp>
#include <cstdint> // uint
#include <cassert>
struct A {
uint32_t a;
uint8_t b;
uint16_t c;
};
struct map{
A& aa_;
template<class String>
decltype(auto) operator[](String s) const{
using namespace boost::hana::literals;
if constexpr(s == "field1"_s) return (decltype(aa_.a)&)(aa_.a);
if constexpr(s == "field2"_s) return (decltype(aa_.b)&)(aa_.b);
if constexpr(s == "field3"_s) return (decltype(aa_.c)&)(aa_.c);
}
};
using namespace boost::hana::literals;
void f(uint32_t& a){ a = 3.;}
int main(){
A aa{1,2,3};
map mymap{aa};
mymap["field1"_s] = 4; assert(aa.a == 4);
mymap["field2"_s] = 5; assert(aa.b == 5);
mymap["field3"_s] = 6; assert(aa.c == 6);
auto c = mymap["field3"_s]; assert( c == aa.c );
mymap["blabla"_s]; // is void (not a compile error)
assert( map{aa}["field1"_s] == 4 );
}
From this you can walk backwards and perhaps figure out a C++14, the challenge ahead is that you have to implement your own compile time string literals and equality.
In other words, reimplement your own Hana string strings: https://www.boost.org/doc/libs/1_61_0/libs/hana/doc/html/namespaceboost_1_1hana_1_1literals.html
The simple solution is that if you have a script that can generate the struct then you can have your script generate your setters.
Start with a simple function interface.
struct A {
uint32_t a;
uint8_t b;
uint16_t c;
};
typedef std::function<void(A &,string)> SetterType
Then create a lookup table
std::map<std::string,SetterType> Lookup;
and for each field, using your script, generate a parser and setter;
void A_a(A & data, std::string input){
data.a = std::stoi(input);
}
and then
Lookup["a"] = &A_a;
and use it like
Lookup["a"]("10");
If you can't modify the generating script then maybe you can use a third party parser such as swig or clang to read your structures and generate you a parse tree that you can then use to generate your lookup tables.
Or just use a system that already maps strings to C++. A C++ JSON generator.
https://nlohmann.github.io/json/

callback in C++ struct

I have been trying to implement a callback function in c++. Within a class, I have a struct, a number of methods, and a method that creates an instance of the struct with one of the other methods as its argument.
The struct has many other variables, but an illustration is depicted here:
class MYCLASS
{
public:
MYCLASS();
struct TEST{
std::function<int(int)> foo;
};
int plus(int x){
return x + 1;
}
int minus(int x){
return x - 1;
}
void sim(){
TEST T; // make an instance of TEST
T.foo = plus(5); // assign TEST.foo a function (plus or minus)
T.foo(); // call the method we assigned
}
};
Within the sim method, I want to create an instance of test and give it either plus or minus, depending on some criterion. Both lines where I try and give the instance T a plus function and subsequently call it are incorrect.
If you want to delay the call to T.foo, then you could use a lambda like this:
T.foo = [this](int x) { return plus(x); };
T.foo(5);
Option - 1
If the member functions plus() and minus() are simple enough like you have shown, you can make them as lambda functions inside the struct TEST.
Since the capture-less lambdas can be stored in typed function pointers, the following will do what you want.
See live demo
#include <iostream>
class MYCLASS
{
int m_var = 5; // just for demonstration
public:
MYCLASS() = default;
struct TEST
{
using fPtrType = int(*)(int); // function pointer type
const fPtrType foo1 = [](int x) { return x + 1; }; // plus function
const fPtrType foo2 = [](int x) { return x - 1; }; // minus function
};
void sim()
{
TEST T;
std::cout << "Answer from int PLUS(int): " << T.foo1(m_var) << std::endl;
std::cout << "Answer from int MINUS(int): " << T.foo2(m_var) << std::endl;
}
};
Option - 2
If the above alter a lot in your code, use typed function pointer again for member functions and do as follows; which will avoid unnecessary copying(by capturing) the class instance to the lambda and template instantiation and other performance issues comes along with std::function as well.
See live demo
#include <iostream>
class MYCLASS
{
using fPtrType = int(MYCLASS::*)(int); // class member function pointer type
public:
MYCLASS() = default;
struct TEST { fPtrType foo = nullptr; };
int plus(int x) { return x + 1; }
int minus(int x) { return x - 1; }
void sim()
{
TEST T;
T.foo = &MYCLASS::plus; // now you can
std::cout << "Answer from int PLUS(int): " << (this->*T.MYCLASS::TEST::foo)(5) << std::endl;
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ syntax would be a bit ugly
// later same ptr variable for minus()
T.foo = &MYCLASS::minus;
int answer = (this->*T.MYCLASS::TEST::foo)(5);
std::cout << "Answer from int MINUS(int): " << answer << std::endl;
}
};
int main()
{
MYCLASS obj;
obj.sim();
return 0;
}
Output:
Answer from int PLUS(int): 6
Answer from int MINUS(int): 4

type defined in free function, accessible through auto outside. Language Bug or Feature?

Let's define a class inside a free function, and access it outside:
#include <iostream>
auto myFunc(){
class MyType{public: int i = 0; int j = 1;};
return MyType();
}
int main() {
auto my_type = myFunc();
std::cout << my_type.i << " " << my_type.j << "\n";
return 0;
}
It compiles, run as expected:
0 1
The name MyType is properly hidden:
if we replace auto, the following won't compile:
int main() {
MyType my_type = myFunc();
std::cout << my_type.i << " " << my_type.j << "\n";
return 0;
}
What does the standard say about it?
How to prevent it? The following code did not help:
namespace{
auto myFunc(){
class MyType{public: int i = 0; int j = 1;};
return MyType();
}
}
int main() {
auto my_type = myFunc();
std::cout << my_type.i << " " << my_type.j << "\n";
// your code goes here
return 0;
}
The standard doesn't say anything about this specifically, except that — as you've already pointed out — it's the name that has a scope, not the type. Use of auto bypasses the type's name, giving you a way to get at the type regardless of the name's scope.
It's kind of similar to how making a nested class private doesn't mean you can't use instances of it, only that you can't name it outside of the encapsulating class's scope.
I don't see how you'd "prevent" it, nor why you'd want to.

Changing VTBL of existing object "on the fly", dynamic subclassing

Consider the following setup.
Base class:
class Thing {
int f1;
int f2;
Thing(NO_INIT) {}
Thing(int n1 = 0, int n2 = 0): f1(n1),f2(n2) {}
virtual ~Thing() {}
virtual void doAction1() {}
virtual const char* type_name() { return "Thing"; }
}
And derived classes that are different only by implementation of methods above:
class Summator {
Summator(NO_INIT):Thing(NO_INIT) {}
virtual void doAction1() override { f1 += f2; }
virtual const char* type_name() override { return "Summator"; }
}
class Substractor {
Substractor(NO_INIT):Thing(NO_INIT) {}
virtual void doAction1() override { f1 -= f2; }
virtual const char* type_name() override { return "Substractor"; }
}
The task I have requires ability to change class (VTBL in this case) of existing objects on the fly. This is known as dynamic subclassing if I am not mistaken.
So I came up with the following function:
// marker used in inplace CTORs
struct NO_INIT {};
template <typename TO_T>
inline TO_T* turn_thing_to(Thing* p)
{
return ::new(p) TO_T(NO_INIT());
}
that does just that - it uses inplace new to construct one object in place of another. Effectively this just changes vtbl pointer in objects. So this code works as expected:
Thing* thing = new Thing();
cout << thing->type_name() << endl; // "Thing"
turn_thing_to<Summator>(thing);
cout << thing->type_name() << endl; // "Summator"
turn_thing_to<Substractor>(thing);
cout << thing->type_name() << endl; // "Substractor"
The only major problems I have with this approach is that
a) each derived classes shall have special constructors like Thing(NO_INIT) {} that shall do precisely nothing. And b) if I will want to add members like std::string to the Thing they will not work - only types that have NO_INIT constructors by themselves are allowed as members of the Thing.
Question: is there a better solution for such dynamic subclassing that solves 'a' and 'b' problems ? I have a feeling that std::move semantic may help to solve 'b' somehow but not sure.
Here is the ideone of the code.
(Already answered at RSDN http://rsdn.ru/forum/cpp/5437990.1)
There is a tricky way:
struct Base
{
int x, y, z;
Base(int i) : x(i), y(i+i), z(i*i) {}
virtual void whoami() { printf("%p base %d %d %d\n", this, x, y, z); }
};
struct Derived : Base
{
Derived(Base&& b) : Base(b) {}
virtual void whoami() { printf("%p derived %d %d %d\n", this, x, y, z); }
};
int main()
{
Base b(3);
Base* p = &b;
b.whoami();
p->whoami();
assert(sizeof(Base)==sizeof(Derived));
Base t(std::move(b));
Derived* d = new(&b)Derived(std::move(t));
printf("-----\n");
b.whoami(); // the compiler still believes it is Base, and calls Base::whoami
p->whoami(); // here it calls virtual function, that is, Derived::whoami
d->whoami();
};
Of course, it's UB.
For your code, I'm not 100% sure it's valid according to the standard.
I think the usage of the placement new which doesn't initialize any member variables, so to preserve previous class state, is undefined behavior in C++. Imagine there is a debug placement new which will initialize all uninitialized member variable into 0xCC.
union is a better solution in this case. However, it does seem that you are implementing the strategy pattern. If so, please use the strategy pattern, which will make code a lot easier to understand & maintain.
Note: the virtual should be removed when using union.
Adding it is ill-formed as mentioned by Mehrdad, because introducing virtual function doesn't meet standard layout.
example
#include <iostream>
#include <string>
using namespace std;
class Thing {
int a;
public:
Thing(int v = 0): a (v) {}
const char * type_name(){ return "Thing"; }
int value() { return a; }
};
class OtherThing : public Thing {
public:
OtherThing(int v): Thing(v) {}
const char * type_name() { return "Other Thing"; }
};
union Something {
Something(int v) : t(v) {}
Thing t;
OtherThing ot;
};
int main() {
Something sth{42};
std::cout << sth.t.type_name() << "\n";
std::cout << sth.t.value() << "\n";
std::cout << sth.ot.type_name() << "\n";
std::cout << sth.ot.value() << "\n";
return 0;
}
As mentioned in the standard:
In a union, at most one of the non-static data members can be active at any time, that is, the value of at most one of the non-static data members can be stored in a union at any time. [ Note: One special guarantee is made in order to simplify the use of unions: If a standard-layout union contains several standard-layout structs that share a common initial sequence (9.2), and if an object of this standard-layout union type contains one of the standard-layout structs, it is permitted to inspect the common initial sequence of any of standard-layout struct members; see 9.2. — end note ]
Question: is there a better solution for such dynamic subclassing that solves 'a' and 'b' problems ?
If you have fixed set of sub-classes then you may consider using algebraic data type like boost::variant. Store shared data separately and place all varying parts into variant.
Properties of this approach:
naturally works with fixed set of "sub-classes". (though, some kind of type-erased class can be placed into variant and set would become open)
dispatch is done via switch on small integral tag. Sizeof tag can be minimized to one char. If your "sub-classes" are empty - then there will be small additional overhead (depends on alignment), because boost::variant does not perform empty-base-optimization.
"Sub-classes" can have arbitrary internal data. Such data from different "sub-classes" will be placed in one aligned_storage.
You can make bunch of operations with "sub-class" using only one dispatch per batch, while in general case with virtual or indirect calls dispatch will be per-call. Also, calling method from inside "sub-class" will not have indirection, while with virtual calls you should play with final keyword to try to achieve this.
self to base shared data should be passed explicitly.
Ok, here is proof-of-concept:
struct ThingData
{
int f1;
int f2;
};
struct Summator
{
void doAction1(ThingData &self) { self.f1 += self.f2; }
const char* type_name() { return "Summator"; }
};
struct Substractor
{
void doAction1(ThingData &self) { self.f1 -= self.f2; }
const char* type_name() { return "Substractor"; }
};
using Thing = SubVariant<ThingData, Summator, Substractor>;
int main()
{
auto test = [](auto &self, auto &sub)
{
sub.doAction1(self);
cout << sub.type_name() << " " << self.f1 << " " << self.f2 << endl;
};
Thing x = {{5, 7}, Summator{}};
apply(test, x);
x.sub = Substractor{};
apply(test, x);
cout << "size: " << sizeof(x.sub) << endl;
}
Output is:
Summator 12 7
Substractor 5 7
size: 2
LIVE DEMO on Coliru
Full Code (it uses some C++14 features, but can be mechanically converted into C++11):
#define BOOST_VARIANT_MINIMIZE_SIZE
#include <boost/variant.hpp>
#include <type_traits>
#include <functional>
#include <iostream>
#include <utility>
using namespace std;
/****************************************************************/
// Boost.Variant requires result_type:
template<typename T, typename F>
struct ResultType
{
mutable F f;
using result_type = T;
template<typename ...Args> T operator()(Args&& ...args) const
{
return f(forward<Args>(args)...);
}
};
template<typename T, typename F>
auto make_result_type(F &&f)
{
return ResultType<T, typename decay<F>::type>{forward<F>(f)};
}
/****************************************************************/
// Proof-of-Concept
template<typename Base, typename ...Ts>
struct SubVariant
{
Base shared_data;
boost::variant<Ts...> sub;
template<typename Visitor>
friend auto apply(Visitor visitor, SubVariant &operand)
{
using result_type = typename common_type
<
decltype( visitor(shared_data, declval<Ts&>()) )...
>::type;
return boost::apply_visitor(make_result_type<result_type>([&](auto &x)
{
return visitor(operand.shared_data, x);
}), operand.sub);
}
};
/****************************************************************/
// Demo:
struct ThingData
{
int f1;
int f2;
};
struct Summator
{
void doAction1(ThingData &self) { self.f1 += self.f2; }
const char* type_name() { return "Summator"; }
};
struct Substractor
{
void doAction1(ThingData &self) { self.f1 -= self.f2; }
const char* type_name() { return "Substractor"; }
};
using Thing = SubVariant<ThingData, Summator, Substractor>;
int main()
{
auto test = [](auto &self, auto &sub)
{
sub.doAction1(self);
cout << sub.type_name() << " " << self.f1 << " " << self.f2 << endl;
};
Thing x = {{5, 7}, Summator{}};
apply(test, x);
x.sub = Substractor{};
apply(test, x);
cout << "size: " << sizeof(x.sub) << endl;
}
use return new(p) static_cast<TO_T&&>(*p);
Here is a good resource regarding move semantics: What are move semantics?
You simply can't legally "change" the class of an object in C++.
However if you mention why you need this, we might be able to suggest alternatives. I can think of these:
Do v-tables "manually". In other words, each object of a given class should have a pointer to a table of function pointers that describes the behavior of the class. To modify the behavior of this class of objects, you modify the function pointers. Pretty painful, but that's the whole point of v-tables: to abstract this away from you.
Use discriminated unions (variant, etc.) to nest objects of potentially different types inside the same kind of object. I'm not sure if this is the right approach for you though.
Do something implementation-specific. You can probably find the v-table formats online for whatever implementation you're using, but you're stepping into the realm of undefined behavior here so you're playing with fire. And it most likely won't work on another compiler.
You should be able to reuse data by separating it from your Thing class. Something like this:
template <class TData, class TBehaviourBase>
class StateStorageable {
struct StateStorage {
typedef typename std::aligned_storage<sizeof(TData), alignof(TData)>::type DataStorage;
DataStorage data_storage;
typedef typename std::aligned_storage<sizeof(TBehaviourBase), alignof(TBehaviourBase)>::type BehaviourStorage;
BehaviourStorage behaviour_storage;
static constexpr TData *data(TBehaviourBase * behaviour) {
return reinterpret_cast<TData *>(
reinterpret_cast<char *>(behaviour) -
(offsetof(StateStorage, behaviour_storage) -
offsetof(StateStorage, data_storage)));
}
};
public:
template <class ...Args>
static TBehaviourBase * create(Args&&... args) {
auto storage = ::new StateStorage;
::new(&storage->data_storage) TData(std::forward<Args>(args)...);
return ::new(&storage->behaviour_storage) TBehaviourBase;
}
static void destroy(TBehaviourBase * behaviour) {
auto storage = reinterpret_cast<StateStorage *>(
reinterpret_cast<char *>(behaviour) -
offsetof(StateStorage, behaviour_storage));
::delete storage;
}
protected:
StateStorageable() = default;
inline TData *data() {
return StateStorage::data(static_cast<TBehaviourBase *>(this));
}
};
struct Data {
int a;
};
class Thing : public StateStorageable<Data, Thing> {
public:
virtual const char * type_name(){ return "Thing"; }
virtual int value() { return data()->a; }
};
Data is guaranteed to be leaved intact when you change Thing to other type and offsets should be calculated at compile-time so performance shouldn't be affected.
With a propert set of static_assert's you should be able to ensure that all offsets are correct and there is enough storage for holding your types. Now you only need to change the way you create and destroy your Things.
int main() {
Thing * thing = Thing::create(Data{42});
std::cout << thing->type_name() << "\n";
std::cout << thing->value() << "\n";
turn_thing_to<OtherThing>(thing);
std::cout << thing->type_name() << "\n";
std::cout << thing->value() << "\n";
Thing::destroy(thing);
return 0;
}
There is still UB because of not reassigning thing which can be fixed by using result of turn_thing_to
int main() {
...
thing = turn_thing_to<OtherThing>(thing);
...
}
Here is one more solution
While it slightly less optimal (uses intermediate storage and CPU cycles to invoke moving ctors) it does not change semantic of original task.
#include <iostream>
#include <string>
#include <memory>
using namespace std;
struct A
{
int x;
std::string y;
A(int x, std::string y) : x(x), y(y) {}
A(A&& a) : x(std::move(a.x)), y(std::move(a.y)) {}
virtual const char* who() const { return "A"; }
void show() const { std::cout << (void const*)this << " " << who() << " " << x << " [" << y << "]" << std::endl; }
};
struct B : A
{
virtual const char* who() const { return "B"; }
B(A&& a) : A(std::move(a)) {}
};
template<class TO_T>
inline TO_T* turn_A_to(A* a) {
A temp(std::move(*a));
a->~A();
return new(a) B(std::move(temp));
}
int main()
{
A* pa = new A(123, "text");
pa->show(); // 0xbfbefa58 A 123 [text]
turn_A_to<B>(pa);
pa->show(); // 0xbfbefa58 B 123 [text]
}
and its ideone.
The solution is derived from idea expressed by Nickolay Merkin below.
But he suspect UB somewhere in turn_A_to<>().
I have the same problem, and while I'm not using it, one solution I thought of is to have a single class and make the methods switches based on a "item type" number in the class. Changing type is as easy as changing the type number.
class OneClass {
int iType;
const char* Wears() {
switch ( iType ) {
case ClarkKent:
return "glasses";
case Superman:
return "cape";
}
}
}
:
:
OneClass person;
person.iType = ClarkKent;
printf( "now wearing %s\n", person.Wears() );
person.iType = Superman;
printf( "now wearing %s\n", person.Wears() );