Bizarre : pointer as template parameter

P

persres

Hi,
I am using visual studio 2008.

I have the following code:

typedef int *PINT;

template<class T>
void foo(const T src)
{
}

template<>
void foo<int *> (const int * src)
{
} // line 43

int main()
{
int a;
foo(&a);
}

file.cpp (43) :error C2912: explicit specialization; 'void
foo<int*>(const int *)' is not a specialization of a function
template.

If I were to replace int * with PINT, it compiles.
Any explanations please.
 
P

persres

Hi,
   I am using visual studio 2008.

I have the following code:

typedef int *PINT;

template<class T>
  void  foo(const T src)
{

}

template<>
  void foo<int *> (const int * src)
{

} // line 43

int main()
{
int a;
foo(&a);

}

file.cpp (43) :error C2912: explicit specialization; 'void
foo<int*>(const int *)' is not a specialization of a function
template.

If I were to replace int * with PINT, it compiles.
Any explanations please.

Also this.

template<class T1>
inline const T1 foo(T1 t)
{

return T1();
}

typedef double *PDOUBLE;
template<>
inline const unsigned int * foo <unsigned int *>(unsigned int *i)
{
return i+1;
}
int main()
{
int a = 0;
foo(&a);
}

error C2785: 'const T1 foo(T1)' and 'const unsigned int *foo(unsigned
int *)' have different return types.

Please explain the issues if possible.
 
A

Alf P. Steinbach /Usenet

* (e-mail address removed), on 27.08.2010 13:39:
This is bad in two ways. First, don't typedef pointer types (except for the
purpuse of abstracting away the pointer nature). Second, reserve ALL UPPERCASE
for macros, don't use for anything else.



'const PINT' is not the same as 'const int*'.

'const PINT' is the same as 'PINT const' is the same as 'int *const'.

If you adopt the convention of writing 'const' /after/ the type name then you
avoid most of these problems.


[snip extra example]


Cheers & hth.,

- Alf
 
P

persres

* (e-mail address removed), on 27.08.2010 13:39:

This is bad in two ways. First, don't typedef pointer types (except for the
purpuse of abstracting away the pointer nature). Second, reserve ALL UPPERCASE
for macros, don't use for anything else.





'const PINT' is not the same as 'const int*'.

'const PINT' is the same as 'PINT const' is the same as 'int *const'.

If you adopt the convention of writing 'const' /after/ the type name then you
avoid most of these problems.

[snip extra example]

Cheers & hth.,

- Alf
Do you know anything about the template errors?
 
A

Alf P. Steinbach /Usenet

* (e-mail address removed), on 27.08.2010 13:57:
Do you know anything about the template errors?

I only addressed the errors that you showed.

If you have additional errors in some code on your screen, just post that code
and the results (perhaps also with explanation of what you expected instead).

By the way, please don't quote signatures.


Cheers & hth.,

- Alf
 
P

persres

* (e-mail address removed), on 27.08.2010 13:57:







I only addressed the errors that you showed.

If you have additional errors in some code on your screen, just post that code
and the results (perhaps also with explanation of what you expected instead).

By the way, please don't quote signatures.

Cheers & hth.,

- Alf

--

I still don't understand how to get rid of the compile errors.
Can you get rid of the errors.
Thanks
 
V

Victor Bazarov

* (e-mail address removed), on 27.08.2010 13:39:
On 27 Aug, 12:19, "(e-mail address removed)"<[email protected]>
wrote:
Hi,
I am using visual studio 2008.
I have the following code:
typedef int *PINT;

This is bad in two ways. First, don't typedef pointer types (except for the
purpuse of abstracting away the pointer nature). Second, reserve ALL UPPERCASE
for macros, don't use for anything else.




template<class T>
void foo(const T src)
{

template<>
void foo<int *> (const int * src)
{
} // line 43
int main()
{
int a;
foo(&a);

file.cpp (43) :error C2912: explicit specialization; 'void
foo<int*>(const int *)' is not a specialization of a function
template.
If I were to replace int * with PINT, it compiles.
Any explanations please.

'const PINT' is not the same as 'const int*'.

'const PINT' is the same as 'PINT const' is the same as 'int *const'.

If you adopt the convention of writing 'const' /after/ the type name then you
avoid most of these problems.

[snip extra example]

Cheers& hth.,

- Alf
Do you know anything about the template errors?

Nah, he just pretends :)...

The error, as Alf was trying to lead you, is due to your
misunderstanding of what typedef does and what 'const' qualifiies when
used. In your code

The argument of the "specialization" cannot be matched to make that the
specialization. If you specialize your 'foo' template on 'int*', the
specialization would look like this:

template<>
void foo<int*> (int * const src ) ...

Can you spot a *single* significant difference? Once you find it,
reread Alf's reply. Do you get it now? If not, reread it again until
you do.

V
 
P

persres

* (e-mail address removed), on 27.08.2010 13:39:
On 27 Aug, 12:19, "(e-mail address removed)"<[email protected]>
wrote:
Hi,
     I am using visual studio 2008.
I have the following code:
typedef int *PINT;
This is bad in two ways. First, don't typedef pointer types (except for the
purpuse of abstracting away the pointer nature). Second, reserve ALL UPPERCASE
for macros, don't use for anything else.
template<class T>
    void  foo(const T src)
{
}
template<>
    void foo<int *>    (const int * src)
{
} // line 43
int main()
{
int a;
foo(&a);
}
file.cpp (43) :error C2912: explicit specialization; 'void
foo<int*>(const int *)' is not a specialization of a function
template.
If I were to replace int * with PINT, it compiles.
Any explanations please.
'const PINT' is not the same as 'const int*'.
'const PINT' is the same as 'PINT const' is the same as 'int *const'.
If you adopt the convention of writing 'const' /after/ the type name then you
avoid most of these problems.
[snip extra example]
Cheers&  hth.,
- Alf
Do you know anything about the template errors?

Nah, he just pretends :)...

The error, as Alf was trying to lead you, is due to your
misunderstanding of what typedef does and what 'const' qualifiies when
used.  In your code

 >>>> template<class T>
 >>>>     void  foo(const T src)
 >>>> {
 >>
 >>>> }
 >>
 >>>> template<>
 >>>>     void foo<int *>    (const int * src)

The argument of the "specialization" cannot be matched to make that the
specialization.  If you specialize your 'foo' template on 'int*', the
specialization would look like this:

     template<>
         void foo<int*> (int * const src ) ...

Can you spot a *single* significant difference?  Once you find it,
reread Alf's reply.  Do you get it now?  If not, reread it again until
you do.

V

ok.
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top