| Thread Closed |
Is this ANSI C++? |
Share Thread | Thread Tools |
| Oct15-05, 02:40 PM | #1 |
|
|
Is this ANSI C++?
Allow me to present a simplified version of what I'm doing: (yes, I have a reason to do this.
)Code:
class wrapper
{
private:
int &v;
public:
wrapper(int &value):v(value) {}
wrapper& operator=(int value) {
v = value;
return *this;
}
// This is the questionable bit:
operator wrapper&() { return *this; }
};
wrapper make_wrapper(int &value)
{
return wrapper(value);
}
template <typename T>
void foo(T &t)
{
t = 1;
}
int main()
{
int x;
foo(make_wrapper(x));
}
I wish to create a class that acts as a reference type. I would like to be able to pass my custom reference type into any template function that expects an actual reference. But here's the problem: C++ doesn't let you make a non-constant reference to a temporary object. Since it would be rather painful for me to overload every function I might ever want to call to take any combination of the reference types I'm writing, and for every function I develop to have multiple versions, I would like a more automatic approach. Thus, I provided a conversion into (wrapper&). g++, at least, is happy with it. Is this valid C++? |
| Oct15-05, 02:59 PM | #2 |
|
|
hmmm... well, if gcc has no issues with it then it must work ok. I know gcc is not yet perfect with respect to the ANSI standard, but oh well.
|
| Oct15-05, 05:13 PM | #3 |
|
|
Code:
class wrapper
{
private:
int &v;
public:
wrapper(int &value):v(value) {}
wrapper& operator=(int value) {
v = value;
return *this;
}
// This is the questionable bit:
operator wrapper&() { return *this; }
};
wrapper make_wrapper(int &value)
{
return wrapper(value);
}
template <typename T>
void foo(T &t)
{
t = 1;
}
int main()
{
int x;
foo(make_wrapper(x));
}
[/QUOTE] operator wrapper&() { return *this; } i dont understand this, can you return an operator?(not that i know of) if you can the this pointer is a pointer to a class and not an operator. maybe you mean wrapper operator&(){ return *this; } |
| Oct15-05, 05:28 PM | #4 |
|
|
Is this ANSI C++?
operator wrapper& is a conversion operator, like operator int, or operator char. It allows you to convert an object of type wrapper into an object of type wrapper&
|
| Oct15-05, 05:32 PM | #5 |
|
|
|
| Oct15-05, 05:35 PM | #6 |
|
|
hmmm i havnt seen this before an cant seem to find anything in stroustrup's book have to got a link to an example or something please thanks.
|
| Oct15-05, 05:37 PM | #7 |
|
|
hmmm i havnt seen this before an cant seem to find anything in stroustrup's book have to got a link to an example or something please thanks.
edit. hmmm now i see, sorry about that |
| Oct15-05, 08:55 PM | #8 |
|
|
dmail try looking under type casting...cuz thats what your basically doing
you can overload int and double's i believe...i use the typdef double REAL and you can basically do (REAL*)vector...to get teh ntuple outta class vector. so if you store an array in teh class you can do operator REAL*() { return _a; } where _a is teh array. and thus you leave out the remainder of the class. why do you need ansi standards hurkyl...can't you test it out on your compiler? |
| Oct16-05, 06:07 AM | #9 |
|
|
Because it's nice to have code that relies only on defined behavior.
This isn't solely for personal use -- I expect others to be able to use this as well.
|
| Oct16-05, 08:29 AM | #10 |
|
Recognitions:
|
If you compile with gcc you can pass the -ansi parameter and it force the standard. You may also need to use the __STRICT_ANSI__ macro.
|
| Oct16-05, 08:43 AM | #11 |
|
|
Well, I suspect the syntax is valid, I'm more worried about the semantics -- whether I can be guaranteed that my object will live while I pass the reference around... -ansi doesn't guarantee me that, does it?
|
| Oct16-05, 08:44 AM | #12 |
|
|
Oh wait... I figured out how I can figure this out. I just need to make the destructor do something... gcc will surely put the destructor call exactly where it's supposed to go, right? Or is gcc allowed to wait for a more convenient time?
|
| Oct26-05, 03:50 PM | #13 |
|
|
Code:
// This is the questionable bit:
operator wrapper&() { return *this; }
Other than that, returning *this is perfectly fine, IIRC. You just need to make sure that you don't access the memory after your object has been deleted. |
| Oct26-05, 06:13 PM | #14 |
|
|
|
| Oct26-05, 10:44 PM | #15 |
|
|
I think what you're doing is undefined behaviour. Your operator wrapper& is fine, but I don't think it's valid to return a reference to a temporary; I think that the temporary is created on the stack, and so any reference to it becomes invalid as soon as make_wrapper returns. I'm not sure, though. It's been a while since I had to think about the object creation rules in C++. |
| Oct29-05, 07:17 AM | #16 |
|
|
|
| Thread Closed |
| Thread Tools | |
Similar Threads for: Is this ANSI C++?
|
||||
| Thread | Forum | Replies | ||
| Military Standards / ANSI / Mil Spec | Mechanical Engineering | 9 | ||
| Lets talk all about ANSI C | Programming & Comp Sci | 6 | ||