this pointer

J

Jersay Ballot

Could yopu explain why VC passes but gnu fails the following simple
function ? thanks

template<class T>
T& getref(T&)
{
return *this;
}
 
I

Ivan Novick

Jersay said:
Could yopu explain why VC passes but gnu fails the following simple
function ? thanks

template<class T>
T& getref(T&)
{
return *this;
}
Is our function supposed to be part of a class? You can not use the
this pointer in a non-member function.
 
J

Jersay Ballot

Ian Collins $B$N%a%C%;!<%8(B:
Define 'fails' and provide some context.

Thank Sir for everything,
gnu says "this is invalidly used in a non-member function", VC doesn't
report any error.
I wonder simply how checking member or non-member is carried out.
 
A

Alf P. Steinbach

* Jersay Ballot:
Could yopu explain why VC passes but gnu fails the following simple
function ? thanks

template<class T>
T& getref(T&)
{
return *this;
}

Post a minimal program that exhibits the problem -- see the FAQ on how
to post (do it).
 
I

Ian Collins

Jersay said:
Ian Collins $B$N%a%C%;!<%8(B:


Thank Sir for everything,
gnu says "this is invalidly used in a non-member function", VC doesn't
report any error.
I wonder simply how checking member or non-member is carried out.
Then either VC is broken, or you aren't instantiating the function. gcc
tends to parse stuff it doesn't have to according to the standard
(templates that aren't instantiated).

You realy should post a complete example.
 
J

John Carson

Jersay Ballot said:
Ian Collins $B$N%a%C%;!<%8(B:

Thank Sir for everything,
gnu says "this is invalidly used in a non-member function", VC doesn't
report any error.
I wonder simply how checking member or non-member is carried out.


Try calling the function and you will get an error. In a departure from the
Standard, VC++ doesn't implement two-phase lookup for templates, so you
often have to call functions/declare objects to get an error.
 
R

Ron Natalie

Jersay said:
Could yopu explain why VC passes but gnu fails the following simple
function ? thanks

template<class T>
T& getref(T&)
{
return *this;
}
There's no this pointer in things that aren't non-static member functions.
 
R

Ron Natalie

Jersay said:
Ian Collins $B$N%a%C%;!<%8(B:

Thank Sir for everything,
gnu says "this is invalidly used in a non-member function", VC doesn't
report any error.
I wonder simply how checking member or non-member is carried out.
The compiler is free to not notice the semantic error (this used
inappropriately) until you actually instantiate the template.

If added a
getref(3);

or something similar, I'm sure VC++ would have barfed.
 
J

Jersay Ballot

Ron Natalie $B$N%a%C%;!<%8(B:
The compiler is free to not notice the semantic error (this used
inappropriately) until you actually instantiate the template.

If added a
getref(3);

or something similar, I'm sure VC++ would have barfed.

Thank you for your suggestion. I want to put a class there not the
integer, so I can reference class objetcs via that function although I
still don't know if there is a way to populate all class members into
strings or normal output.
About the template instantiation as what you explain certainly I never
apply as a solution to my current problem.
 
I

Ian Collins

John said:
Try calling the function and you will get an error. In a departure from the
Standard, VC++ doesn't implement two-phase lookup for templates, so you
often have to call functions/declare objects to get an error.
Isn't it the other way round? gcc performs too much checking of
templates that aren't instantiated.
 
J

John Carson

Ian Collins said:
Isn't it the other way round? gcc performs too much checking of
templates that aren't instantiated.

You mean that it is gcc and VC++ that is not standard compliant? I believe
not. I'm not familiar with the details of what should be checked at each
phase, but VC++ is known to be non-compliant by design with this aspect of
the standard. Like gcc, Comeau online

http://www.comeaucomputing.com/tryitout/

refuses to compile the code.
 
I

Ian Collins

John said:
You mean that it is gcc and VC++ that is not standard compliant? I believe
not. I'm not familiar with the details of what should be checked at each
phase, but VC++ is known to be non-compliant by design with this aspect of
the standard. Like gcc, Comeau online

http://www.comeaucomputing.com/tryitout/

refuses to compile the code.
I can't find the relevant section in the standard (I'd forgotten how
long the template section is!), but I'm sure the compiler is only
supposed to check syntax for unused templates.
 
K

Kai-Uwe Bux

Ian said:
I can't find the relevant section in the standard (I'd forgotten how
long the template section is!), but I'm sure the compiler is only
supposed to check syntax for unused templates.

What do you mean by "check syntax"? Are you saying that a file containing
just

template < typename T >
struct X {
int ( int ] int int [ ) {}
};

is supposed to compile?


Best

Kai-Uwe Bux
 
I

Ian Collins

Kai-Uwe Bux said:
Ian said:
I can't find the relevant section in the standard (I'd forgotten how
long the template section is!), but I'm sure the compiler is only
supposed to check syntax for unused templates.


What do you mean by "check syntax"? Are you saying that a file containing
just

template < typename T >
struct X {
int ( int ] int int [ ) {}
};

is supposed to compile?
I'll have to search the standard again, but if X isn't instantiated, why
should it fail to compile?
 
K

Kai-Uwe Bux

Ian said:
Kai-Uwe Bux said:
Ian said:
I can't find the relevant section in the standard (I'd forgotten how
long the template section is!), but I'm sure the compiler is only
supposed to check syntax for unused templates.


What do you mean by "check syntax"? Are you saying that a file containing
just

template < typename T >
struct X {
int ( int ] int int [ ) {}
};

is supposed to compile?
I'll have to search the standard again, but if X isn't instantiated, why
should it fail to compile?

Well, what about the sequence of tokens not being a well-formed program
[1.3.14] according to the production rules in the grammar of C++? That
alone should make the compiler barff.

A more interesting question arises from the fact that the C++ in not context
free. The amount of checking for semantic constraints may actually depend
on whether a template is instantiated or not (keyword: diagnosable semantic
rules). But pure syntax check is clearly required.


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top