Bizarre : pointer as template parameter

Discussion in 'C++' started by persres@googlemail.com, Aug 27, 2010.

  1. Guest

    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.
    , Aug 27, 2010
    #1
    1. Advertising

  2. Guest

    On 27 Aug, 12:19, "" <>
    wrote:
    > 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.
    , Aug 27, 2010
    #2
    1. Advertising

  3. * , on 27.08.2010 13:39:
    > On 27 Aug, 12:19, ""<>
    > 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

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Aug 27, 2010
    #3
  4. Guest

    On 27 Aug, 12:46, "Alf P. Steinbach /Usenet" <alf.p.steinbach
    > wrote:
    > * , on 27.08.2010 13:39:
    >
    > > On 27 Aug, 12:19, ""<>
    > > 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
    >
    > --
    > blog at <url:http://alfps.wordpress.com>- Hide quoted text -
    >
    > - Show quoted text -

    Do you know anything about the template errors?
    , Aug 27, 2010
    #4
  5. * , on 27.08.2010 13:57:
    > On 27 Aug, 12:46, "Alf P. Steinbach /Usenet"<alf.p.steinbach
    > > wrote:
    >> * , on 27.08.2010 13:39:
    >>
    >>> On 27 Aug, 12:19, ""<>
    >>> 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.

    >
    > 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

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Aug 27, 2010
    #5
  6. Guest

    On 27 Aug, 13:11, "Alf P. Steinbach /Usenet" <alf.p.steinbach
    > wrote:
    > * , on 27.08.2010 13:57:
    >
    >
    >
    >
    >
    > > On 27 Aug, 12:46, "Alf P. Steinbach /Usenet"<alf.p.steinbach
    > > >  wrote:
    > >> * , on 27.08.2010 13:39:

    >
    > >>> On 27 Aug, 12:19, ""<>
    > >>> 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.

    >
    > > 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
    >
    > --


    I still don't understand how to get rid of the compile errors.
    Can you get rid of the errors.
    Thanks
    , Aug 27, 2010
    #6
  7. On 8/27/2010 7:57 AM, wrote:
    > On 27 Aug, 12:46, "Alf P. Steinbach /Usenet"<alf.p.steinbach
    > > wrote:
    >> * , on 27.08.2010 13:39:
    >>
    >>> On 27 Aug, 12:19, ""<>
    >>> 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
    >>
    >> --
    >> blog at<url:http://alfps.wordpress.com>- Hide quoted text -
    >>
    >> - Show quoted text -

    > 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
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 27, 2010
    #7
  8. Guest

    On 27 Aug, 13:20, Victor Bazarov <> wrote:
    > On 8/27/2010 7:57 AM, wrote:
    >
    >
    >
    >
    >
    > > On 27 Aug, 12:46, "Alf P. Steinbach /Usenet"<alf.p.steinbach
    > > >  wrote:
    > >> * , on 27.08.2010 13:39:

    >
    > >>> On 27 Aug, 12:19, ""<>
    > >>> 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

    >
    > >> --
    > >> blog at<url:http://alfps.wordpress.com>- Hide quoted text -

    >
    > >> - Show quoted text -

    > > 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
    > --
    > I do not respond to top-posted replies, please don't ask- Hide quoted text -
    >
    > - Show quoted text -


    ok.
    , Aug 27, 2010
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Amadeus W. M.

    pointer to function template parameter

    Amadeus W. M., Sep 3, 2005, in forum: C++
    Replies:
    4
    Views:
    328
    Amadeus W. M.
    Sep 7, 2005
  2. Vijai Kalyan
    Replies:
    4
    Views:
    683
    Vijai Kalyan
    Nov 8, 2005
  3. Stuart Redmann
    Replies:
    5
    Views:
    462
    Stuart Redmann
    Dec 14, 2007
  4. Taygun Kekec

    Windows OS , Bizarre File Pointer Fact

    Taygun Kekec, Jun 27, 2008, in forum: Python
    Replies:
    2
    Views:
    332
    Matt Nordhoff
    Jun 27, 2008
  5. Isti
    Replies:
    2
    Views:
    688
Loading...

Share This Page