0

Of course, I know about workaround like this (implemented here) by creating my own class and emulate all operators of the native type, but is there a more elegant way to extend some native type?

If you want to ask why:

It could be useful to create a class with runtime checking for overflow. Also I can create a template like this:

template <class T> class LockFreeVolatileValue: public T {...}

which could be used for any types (now it's only for classes).

Related questions:

C++ derive from a native type

Why can't I inherit from int in C++?

Community
  • 1
  • 1
Maxim Kholyavkin
  • 4,463
  • 2
  • 37
  • 82
  • 1
    *It could be useful to create class with runtime checking overflow.* Presumably you would have to implement all the operators with overflow checking so how is it different from *emulate all operators from native type* – ta.speot.is Oct 01 '14 at 07:11
  • 1
    Possible duplicate of [Why can't I inherit from int in C++?](http://stackoverflow.com/questions/2143020/why-cant-i-inherit-from-int-in-c). You actually give a link to the answer of your question, but still ask the question? – DevSolar Oct 01 '14 at 08:03
  • @DevSolar questions are different. It could be possible non standard compiler option. – Maxim Kholyavkin Oct 01 '14 at 09:08
  • 1
    @Speakus: Answers to that question explain quite nicely why *it would not work* without making C++ a *completely different* (and very much non-C-compatible) language, *for very little practical gain*. So why do you think C++ compiler builders should add such an option (and the code to make it work) when any code using that option *wouldn't be C++ to begin with*? – DevSolar Oct 01 '14 at 09:31
  • @DevSolar could you link to explain why it make "completely different (and very much non-C-compatible) language". – Maxim Kholyavkin Oct 02 '14 at 05:09
  • 1
    @Speakus: See [the quote from Bjarne Stroustrup](http://stackoverflow.com/a/2143734/60281): "the C conversion rules are so chaotic that pretending that int, short, etc., are well-behaved ordinary classes is not going to work. They are either C compatible, or they obey the relatively well-behaved C++ rules for classes, but not both." – DevSolar Oct 02 '14 at 11:43
  • i don't ask virtual destructors for classes inherit from native type. Anyway this class could be binary compatible with old C and you can see it from this commit: https://github.com/Speakus/cppFundamentalClass/commit/abce84c012c37f3e10beac2aca3d2bb8486569b9 – Maxim Kholyavkin Oct 02 '14 at 12:21
  • @Speakus So you may think, but you're arguing with Bjarne Stroustrup here. – user207421 Oct 02 '14 at 23:58
  • @EJP I give you code where you can ensure by yourself with binary compatible class based on fundamental type. I agree with with Stroustrup about impossible to do virtual functions in fundamental types, but in this case i don't need virtual functions. – Maxim Kholyavkin Oct 03 '14 at 00:17
  • @Speakus: You might note that Stroustrup is not *talking* about virtual functions... just one quick example, consider the `&&` and `||` operators (which short-circuit for `int`, but don't for `MyClass.operator&&()`)... – DevSolar Oct 06 '14 at 09:11
  • @DevSolar && and || operators is supported too: https://github.com/Speakus/cppFundamentalClass/commit/f84a0278638b88efdf123e4fc5ff3cccc3877200 – Maxim Kholyavkin Oct 06 '14 at 10:34
  • @Speakus: Geeez... how to not put too fine a point to it? Consider what happens when the left-hand side of your `operator||()` is true, *and the right-hand side has a side effect* (e.g. `++i`, or a function call). Consider what happens if the left-hand side is an integer (hint: the side effect does not occur), and what happens if the left-hand side is of your "Primitive" type (hint: the side effect occurs). Generally speaking, I think you're wading waters here that might be a bit too deep yet... – DevSolar Oct 06 '14 at 10:59
  • @DevSolar thank you - i understand what you mean. It's really issue. `val = true; assert(true == val || ++val)` - it changes val if it's class, but it don't change if val is int. – Maxim Kholyavkin Oct 06 '14 at 11:33
  • 1
    @Speakus: Exactly. And that is only one point. Native data types and classes are different kind of beasts, even in Java, and it's far beyond a compiler switch to change that. – DevSolar Oct 06 '14 at 11:50

3 Answers3

3

I'm reasonably familiar with all major C++ compilers, and the answer for them is no.

This would require non-trivial work in constructor initializer lists, class layout, etcetera, without a significant enough benefit.

MSalters
  • 173,980
  • 10
  • 155
  • 350
2

No, the "base" of any class must be a class or struct.

Assuming you actually want to alter the behaviour (e.g. check for over-/underflow in operators +, -, * - I don't think divide is particular bothersome for this particular problem). This means that you have to implement the operators +, - and *, =, +=, -=, *=, ++ and --).

Which you could do by:

template<class T> OverflowDetector
{
private:
   T member;
public:
   T& operator+(T) { ... };
   T& operator-(T) { ... };
   explicit T operator T () { return member; }
};

Then use like:

typedef OverflowDetector<int> safe_int;

safe_int si1, si2;
si1 = 7;
si2 = 18;
int x = int(si2); 

(Yes, you probably need to have some traits type things and a lot more sophistication to stop people doing OverflowDetector<double> or something like that)

You volatile-lockfree class sounds suspiciously similar to std::atomic<T> - I'm sure you had some other ideas, but still, I'm not sure I see the point in inheriting over just implementing using templates...

anatolyg
  • 26,506
  • 9
  • 60
  • 134
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • @Speakus: Yes, but if you implement it yourself, it will be at least as unportable as using C++11, as it will have to be either inline assembler or compiler specific intrinsics (or use C11 atomics, but there are probably fewer compilers that support C11 than C++11). A lot of C++ compilers pre-C++11 does support C++11 style atomics, as that was one of the "early parts", so it was agreed for a fair amount of time before the spec was ratified. – Mats Petersson Oct 02 '14 at 07:44
0

I created wrapper for fundamental types:

https://github.com/Speakus/cppFundamentalClass/blob/master/Primitive.hpp

Which really show why impossible to do class which work same way as native type

example code to fail:

val = 1
assert(1 == val || ++val);
assert(1 == val);

This code fail if val is class and pass if val is int.

Hope it helps somebody who plans to inherit from native types from any reason.

Maxim Kholyavkin
  • 4,463
  • 2
  • 37
  • 82