Home > Archive > VC Language > January 2006 > Error in VC 2005?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Alexander 2006-01-23, 7:08 pm |
| I got error C2664: 'MyFunction' : cannot convert parameter 1 from 'MyClass *'
to 'const MyClass *&'
when I wrote following code:
class MyClass
{
MyClass() {};
virtual ~MyClass() {};
};
template <class T>
bool MyFunction(const T *&p)
{
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
MyClass *MyPointer;
if (MyFunction(MyPointer))
return 0;
else
return 110;
}
Is this an error in VC++ 2005?
Of course I could write it in a different way,
but I can't improve it everywhere... It has also
some effects in STL.
| |
| Igor Tandetnik 2006-01-23, 7:08 pm |
| Alexander <Alexander@discussions.microsoft.com> wrote:
> I got error C2664: 'MyFunction' : cannot convert parameter 1 from
> 'MyClass *' to 'const MyClass *&'
> when I wrote following code:
>
> class MyClass
> {
> MyClass() {};
> virtual ~MyClass() {};
> };
>
> template <class T>
> bool MyFunction(const T *&p)
> {
> return true;
> }
>
> Is this an error in VC++ 2005?
No, the bug is in your code. This conversion is illegal. If it were, it
would allow you to bypass const checks and modify const variables.
Consider:
const MyClass constVar;
bool MyFunction(const MyClass*& p)
{
p = &constVar; // reseat the pointer
return true;
}
MyClass* p;
MyFunction(p); // illegal - p now points to const MyClass
p->Modify(); // which would have allowed one to modify a const variable
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
| |
| Doug Harrison [MVP] 2006-01-23, 7:08 pm |
| On Mon, 23 Jan 2006 09:43:02 -0800, Alexander
<Alexander@discussions.microsoft.com> wrote:
>I got error C2664: 'MyFunction' : cannot convert parameter 1 from 'MyClass *'
>to 'const MyClass *&'
>when I wrote following code:
>
>class MyClass
>{
> MyClass() {};
> virtual ~MyClass() {};
>};
>
>template <class T>
>bool MyFunction(const T *&p)
>{
> return true;
>}
>
>int _tmain(int argc, _TCHAR* argv[])
>{
> MyClass *MyPointer;
> if (MyFunction(MyPointer))
> return 0;
> else
> return 110;
>}
>
>Is this an error in VC++ 2005?
Nope.
>Of course I could write it in a different way,
>but I can't improve it everywhere... It has also
>some effects in STL.
The problem will become evident if you write it like this:
const int X = 0;
void f(const int*& p)
{
p = &X; // Fine
}
void g()
{
int* p;
f(p); // Illegal, which prevents the following oops
*p = 2; // Oops - would write to a const int
}
This is more commonly seen in its pointer guise:
void action(const Foo** cptr);
Foo* fPtr;
action(&fPtr); // Illegal
It's really the same thing.
--
Doug Harrison
Visual C++ MVP
| |
| Alexander 2006-01-23, 7:08 pm |
| Perhaps you didn't see it:
The var was declared const.
Why should I be able to overwite it?
Also VS2003 accepts the same code
without warnings.
| |
| Alexander 2006-01-23, 7:08 pm |
| Thank You.
"Doug Harrison [MVP]" wrote:
> The problem will become evident if you write it like this:
>
> const int X = 0;
>
> void f(const int*& p)
> {
> p = &X; // Fine
> }
>
> void g()
> {
> int* p;
> f(p); // Illegal, which prevents the following oops
> *p = 2; // Oops - would write to a const int
> }
>
This code leads to an access violation when compiled with VS2003.
I understand it (Although I didn't use the code in the described way)
Then I'll bother the people from STL forum... :-)
| |
| Igor Tandetnik 2006-01-23, 7:08 pm |
| Alexander <Alexander@discussions.microsoft.com> wrote:
> Perhaps you didn't see it:
> The var was declared const.
> Why should I be able to overwite it?
There is not a single const variable in your code. In the only place
'const' keyword is used, I see a reference to a non-const pointer to a
const value. Note that the pointer itself is not const, meaning that you
can change it to point to something else. Consider:
int x;
int y;
const int* p = &x; // OK
p = &y; // OK - can change the pointer
*p = 1; // not allowed - can't change the pointee
int* const cp = &x; // OK
cp = &y; // not allowed - can't change the pointer
*cp = 1; // OK - can change the pointee
const int* const ccp = &x; // OK
ccp = &y; // not allowed - can't change either pointer or pointee
*ccp = 1; // not allowed - can't change either pointer or pointee
> Also VS2003 accepts the same code
> without warnings.
If so, then it's a bug in VC7.1 that was fixed in VC8.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
| |
| Doug Harrison [MVP] 2006-01-23, 7:08 pm |
| On Mon, 23 Jan 2006 10:33:03 -0800, Alexander
<Alexander@discussions.microsoft.com> wrote:
>Thank You.
>
>"Doug Harrison [MVP]" wrote:
>
>
>This code leads to an access violation when compiled with VS2003.
If VC7.1 compiles it, VC7.1 has a bug. Note that VC7.1 does emit an error
for the pointer form I gave. VC8 rightly complains about the reference form
as well.
>I understand it (Although I didn't use the code in the described way)
>Then I'll bother the people from STL forum... :-)
No need to bother anyone. :) You need to find another way to express what
you're doing. If you don't assign to the pointer, why do you pass it by
reference? If you show what you're trying to accomplish, someone may be
able to suggest an alternative.
--
Doug Harrison
Visual C++ MVP
| |
| Tim Roberts 2006-01-24, 4:00 am |
| Alexander <Alexander@discussions.microsoft.com> wrote:
>
>"Doug Harrison [MVP]" wrote:
>
>
>This code leads to an access violation when compiled with VS2003.
>I understand it (Although I didn't use the code in the described way)
Of course you did! Your code was *EXACTLY* like this, except you have
"MyClass" instead of "int". It is EXACTLY the same rule that you are
breaking, for EXACTLY the same reason.
I'm still not sure you understand that the problem is in your code. When
you say this:
MyClass * MyPointer;
MyPointer is not a const, nor is it a pointer to a const. You CANNOT pass
it to a function that expects a reference to a const.
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
| |
| Ben Voigt 2006-01-26, 7:08 pm |
|
"Alexander" <Alexander@discussions.microsoft.com> wrote in message
news:58FED9CA-32D6-4BC3-8B49-D24AC57AF46A@microsoft.com...
> Perhaps you didn't see it:
> The var was declared const.
> Why should I be able to overwite it?
bool MyFunction(const T *&p);
p is not const. p points to a const T.
If you are not changing p, then:
bool MyFunction(const T* const& p);
will be suitable, and legal.
Read this as "a read-only reference to a (pointer to const T)"
>
> Also VS2003 accepts the same code
> without warnings.
>
| |
| Doug Harrison [MVP] 2006-01-26, 7:08 pm |
| On Thu, 26 Jan 2006 09:39:25 -0600, "Ben Voigt" <bvoigt@nospam.nospam>
wrote:
>bool MyFunction(const T *&p);
>
>p is not const. p points to a const T.
>
>If you are not changing p, then:
>bool MyFunction(const T* const& p);
>will be suitable, and legal.
>
>Read this as "a read-only reference to a (pointer to const T)"
Then the question is, why use that instead of this:
bool MyFunction(const T* p);
--
Doug Harrison
Visual C++ MVP
|
|
|
|
|