可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
In designing a solution, sometimes it may be convenient to provide wrapper classes for primitive data types. Consider a class that represents a numeric value, be it a double
, a float
, or an int
.
class Number {
private:
double val;
public:
Number(int n) : val(n) { }
Number(float n) : val(n) { }
Number(double n) : val(n) { }
// Assume copy constructors and assignment operators exist
Number& add(const Number& other) {
val += other.val;
return *this;
}
int to_int() const { return (int) val; }
float to_float() const { return (float) val; }
double to_double() const { return val; }
};
Now suppose that I have a function as such:
void advanced_increment(Number& n) {
n.add(1);
}
And I would use this function as such:
Number n(2);
advanced_increment(n); // n = 3
This sounds easy enough. But what if the function was like this?
void primitive_increment(int& n) {
++n;
}
Note that the increment is an example. It is assumed that the function would perform more complicated operations on primitive data types that they should also be able to perform on Number
types without any issues.
How would I use the function exactly as before? As in:
Number n(2);
primitive_increment(n);
How could I make my Number
class compatible with primitive_increment
? How could I create a wrapper class for primitive data types that would be compatible anywhere that these data types are required?
So far, I have only found two solution. One is to create a function such as double& Number::get_value()
and then use it like primitive_increment(n.get_value());
. The second solution is to create implicit conversion methods such as Number::operator int&()
; but these can result in many ambiguous calls and would make the code confusing.
I'm wondering if there is any other solution to implement these types of wrappers and retain their primitive functionality.
Update:
To further clarify, in the actual project, the intent here is to make all data types derived from one base class that is commonly referred to as Object
when designing such solution. A constraint is that no outside library should be used. Therefore, if I have a container that has pointers to the type Object
, it should be able to hold any arbitrary value, primitive or not, and perform any primitive operation that is allowed on Object
. I hope this explains it better.
回答1:
C++11 has explicit operator overloads.
struct silly_wrapper {
int foo;
explicit operator int&() { return foo; }
};
void primitive_increment(int& x) { ++x; }
int main()
{
silly_wrapper x;
primitive_increment(x); // works
x += 1; // doesn't work - can't implicitly cast
}
回答2:
class Number {
enum ValType {DoubleType, IntType} CurType;
union {
double DoubleVal;
int IntVal;
};
public:
Number(int n) : IntVal(n), CurType(int) { }
Number(float n) : DoubleVal(n), CurType(DoubleType) { }
Number(double n) : DoubleVal(n), CurType(DoubleType) { }
// Assume copy constructors and assignment operators exist
Number& add(const Number& other) {
switch(CurType) {
case DoubleType: DoubleVal += other.to_double(); break;
case IntType: IntVal+= other.to_int(); break;
}
return *this;
}
int& to_int() {
switch(CurType) {
case DoubleType: IntVal = DoubleVal; CurType = IntType; break;
//case IntType: DoubleVal = IntVal; CurType = DoubleType; break;
}
return IntVal;
}
const int to_int() const {
switch(CurType) {
case DoubleType: return (int)DoubleVal;
case IntType: return (int)IntVal;
}
}
const float to_float() const {
switch(CurType) {
case DoubleType: return (float)DoubleVal;
case IntType: return (float)IntVal;
}
}
double& to_double() {
switch(CurType) {
//case DoubleType: IntVal = DoubleVal; CurType = IntType; break;
case IntType: DoubleVal = IntVal; CurType = DoubleType; break;
}
return DoubleVal;
}
const double to_double() const {
switch(CurType) {
case DoubleType: return (double)DoubleVal;
case IntType: return (double)IntVal;
}
}
};
void primitive_increment(int& n) {
++n;
}
int main() {
Number pi(3.1415);
primitive_increment(pi.to_int());
//pi now is 4
return 0;
}
I will admit this is quite awkward, and not the ideal situation, but it solves the given problem.
回答3:
Instead of providing it to primitive_increment
. You should overload the ++ operator for your Number
class and increment it that way.
Number& operator++() { ++val; return *this;}
Number& operator+=(const Number& rhs) { val += rhs.Val; return *this;}
Number operator+(const Number& rhs) { Number t(*this); t+=rhs; return t;}
see: Operators in C and C++
回答4:
If your Number
class does not implement a subset of int
, you just cannot do that. It would give wrong results if e.g. your Number
class contains the value INT_MAX
and can hold the value INT_MAX+1
as well. If your Number
class models a subset of int
, then conversion to int
and back is of course an option.
Other than that, your only chance is to rewrite the function to accept Number
objects. Ideally make it a template, so that it can work both with int
and with Number
(as well as with any other current or future class which presents an int
-like interface).
回答5:
Make the conversion operator private and have a friend function do the conversion inside of it.
class silly_wrapper {
private:
int foo;
float bar;
operator int&() { return foo; }
template <typename T>
friend void primitive_increment(T& x) { ++static_cast<int&>(x); }
};
int main()
{
silly_wrapper x;
primitive_increment(x); // works
int i;
primitive_increment(i); // works
int& r = static_cast<int&>(x); // can't convert - operator is private
}
回答6:
Here's an even more bizzare answer I just thought of:
class Number;
template<class par, class base>
class NumberProxy {
base Val;
par* parent;
NumberProxy(par* p, base v) :parent(p), Val(v) {}
NumberProxy(const NumberProxy& rhs) :parent(rhs.parent), Val(rhs.Val) {}
~NumberProxy() { *parent = Val; }
NumberProxy& operator=(const NumberProxy& rhs) {Val = rhs.Val; return *this}
operator base& {return Val;}
};
class Number {
private:
double val;
public:
Number(int n) : val(n) { }
Number(float n) : val(n) { }
Number(double n) : val(n) { }
// Assume copy constructors and assignment operators exist
int to_int() const { return (int) val; }
float to_float() const { return (float) val; }
double to_double() const { return val; }
NumberProxy<Number,int> to_int() { return NumberProxy<Number,int>(this,val); }
NumberProxy<Number,float> to_float() { return NumberProxy<Number,float>(this,val); }
NumberProxy<Number,double> to_double() { return NumberProxy<Number,double>(this,val); }
};
void primitive_increment(int& n) {
++n;
}
int main() {
Number pi(3.1415);
primitive_increment(pi.to_int());
//pi now is 4
return 0;
}
Number.to_int()
returns a NumberProxy<int>
, which is implicity convertable to an int&
, which the function operates on. When the function and expression complete, the temporary NumberProxy<int>
is destroyed, and it's destructor updates it's parent Number
with the updated value. This has the added convenience of only requiring minor modification to the Number
class.
Obviously theres's some danger here, if you call to_N()
twice in the same statement, the two int&'s wont be in sync, or if someone takes a int& past the end of the statement.
回答7:
(This is a bit of a shot in the dark, as I'm not entirely sure how your overall design fits together.)
How about templated free functions:
class IncTagIntegral{};
class IncTagNonintegral{};
template <bool> struct IncTag { typedef IncTagNonintegral type; }
template <> struct IncTag<true> { typedef IncTagIntegral type; }
template <typename T> void inc_impl(T & x, IncTagIntegral)
{
++x;
}
template <typename T> void inc_impl(T & x, IncTagNonintegral)
{
x += T(1);
}
template <typename T> void primitive_increment(T & x)
{
inc_impl<T>(x, typename IncTag<std::is_integral<T>::value>::type());
}
template <> void primitive_increment(Number & x)
{
// whatever
}
This approach may be generalizable to other functions that you need to apply both to existing types and to your own types.
Here's another long shot, this time using type erasure:
struct TEBase
{
virtual void inc() = 0;
}
struct any
{
template <typename T> any(const T &);
void inc() { impl->inc(); }
private:
TEBase * impl;
};
template <typename T> struct TEImpl : public TEBase
{
virtual void inc() { /* implement */ }
// ...
}; // and provide specializations!
template <typename T> any::any<T>(const T & t) : impl(new TEImpl<T>(t)) { }
The key is that you provide different concrete implementations of TEImpl<T>::inc()
by means of specialization, but you can use a.inc()
for any object a
of type any
. You can build additional free-function wrappers on this idea, like void inc(any & a) { a.inc(); }
.