Re: What Standard says on declared only symbols
I try to solve the problem somehow. I can make just a
TreeInfoSerializable which will inherit TreeInfo as well as IWritable
and expect my users to use either one as needed. I could try some
tricks with template metaprogramming (this seems most elegant however
is likely to be worst because the code is in a large project and this
could cause need for a lot of changes). I thought also on some
implementation tricks they would however relay on defined but not
declared functions.
I don't understand this last sentence, because every definition
is also a declaration - would you explain?
I made a mistake. It would relay on functions declared but not
defined and thous a try to serialize TreeInfo of type that does not
allow serialization would result in a link error. Actually compile
error would be fine as well.
I don't know, what you mean with above suggestion concerning
template metaprogramming, but a possible solution is to use
SFINAE here:
struct IWritable {
virtual void write() = 0;
virtual void read() = 0;
};
template <class T>
struct is_writable {
enum {value = std::is_base_of<IWritable, T>::value };
};
template <typename T, class Enable = void>
struct TreeInfo;
template <typename T>
struct TreeInfo<T, typename
std::enable_if<!is_writable<T>::value>::type> {
T d;
};
template <typename T>
struct TreeInfo<T, typename
std::enable_if<is_writable<T>::value>::type> : IWritable {
void write(){
d.write();
}
void read(){
d.read();
}
T d;
};
struct Writable : IWritable {
void write(){}
void read(){}
};
TreeInfo<int> it; // OK, uses the non-writable TreeInfo
TreeInfo<Writable> wt; // OK, uses writable TreeInfo
Yes. I thought on this as well however this requires me to copy the
actual implementation of TreeInfo twice (once for serializable type
and once for non-serializable). Perhaps adding something like
TreeInfoBase would solve the problem. I will think on this. For now I
introduced some minor changes to serialization classes that allowd me
to serialize types that are not IWritable however posses save and load
function as if they were IWritable.
Thanks for help!
Adam Badura
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]