public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* template function specialization trouble - probably a user error, but...
@ 2004-09-17 12:27 Alan Lehotsky
  2004-09-17 12:44 ` Nathan Sidwell
  0 siblings, 1 reply; 5+ messages in thread
From: Alan Lehotsky @ 2004-09-17 12:27 UTC (permalink / raw)
  To: gcc

I'm trying to write a templated function with the following behavior.

Given a type T and an integer width, write a function that creates a
constant of either type T (for POD T)  or of type T::basetype for any
aggregate type having a T::basetype that's a POD.  The constant is
composed of a bitstring of length 'width'.

The following works as long as I don't try to handle aggregate types.
But SFINAE doesn't stop gcc (2.95.3 or 3.4.2) from complaining about
the specialization

      template<> inline T::basetype MASK<T>(int width) ....

producing about 80 lines of errors all told.  I've elided some that I'm
pretty sure are irrelevant.   How can I make the return type of the
function do the right thing here?

=================================================
struct composite {
    int i;
    typedef int basetype;
};

template <class T> inline T MASK (int width) {
    if (width < 0 || width > (sizeof(T)*8))
      return T (0);
    else if (width == (sizeof(T)*8))
      return ~T (0);
    else
      return T(~((~T(0)) << width));
}

// Specialization for composite data types
#ifdef BAD
template <> inline T::basetype
MASK<T>(int width)
{
    return MASK<typename T::basetype>(width);
}
#endif

#include <iostream>

int main ()
{
    int svar = -3;
    std::cout << "MASK<typeof(t)>(5) = " << (int) MASK<typeof (svar)>(5)
<< std::endl;
    std::cout << "MASK<char>(3) = " << (int) MASK<char>(3) << std::endl;
    std::cout << "MASK<unsigned char>(4) = " << (int) MASK<unsigned
char>(4) << std::endl;
    std::cout << "MASK<long long>(70) = " << MASK<long long>(70) <<
std::endl;
    std::cout << "MASK<long long>(64) = " << MASK<long long>(64) <<
std::endl;
#ifdef BAD
    std::cout << "MASK<composite>(7) = " << MASK<composite>(7) <<
std::endl;
#endif
    return 0;
}
=============================================

/tools/linux/gcc-3.4.2/bin/g++ -o mask mask.cxx -DBAD
mask.cxx:18: error: `T' has not been declared
mask.cxx:19: error: expected init-declarator before "MASK"
mask.cxx:19: error: expected `;' before "MASK"
mask.cxx: In function `int main()':
.........
mask.cxx: In function `T MASK(int) [with T = composite]':
mask.cxx:36:   instantiated from here
mask.cxx:9: error: no matching function for call to
`composite::composite(int)'
mask.cxx:2: note: candidates are: composite::composite()
mask.cxx:2: note:                 composite::composite(const composite&)
mask.cxx:11: error: no matching function for call to
`composite::composite(int)'
mask.cxx:2: note: candidates are: composite::composite()
mask.cxx:2: note:                 composite::composite(const composite&)
mask.cxx:13: error: no matching function for call to
`composite::composite(int)'
mask.cxx:2: note: candidates are: composite::composite()
mask.cxx:2: note:                 composite::composite(const composite&)
[apl]aluminum$


Alan Lehotsky - apl@carbondesignsystems.com
Carbon Design Systems, Inc

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: template function specialization trouble - probably a user error, but...
  2004-09-17 12:27 template function specialization trouble - probably a user error, but Alan Lehotsky
@ 2004-09-17 12:44 ` Nathan Sidwell
  2004-09-17 16:39   ` Alexandre Oliva
  0 siblings, 1 reply; 5+ messages in thread
From: Nathan Sidwell @ 2004-09-17 12:44 UTC (permalink / raw)
  To: Alan Lehotsky; +Cc: gcc

Alan Lehotsky wrote:
> I'm trying to write a templated function with the following behavior.
> 
> Given a type T and an integer width, write a function that creates a
> constant of either type T (for POD T)  or of type T::basetype for any
> aggregate type having a T::basetype that's a POD.  The constant is
> composed of a bitstring of length 'width'.
> 
> The following works as long as I don't try to handle aggregate types.
> But SFINAE doesn't stop gcc (2.95.3 or 3.4.2) from complaining about
> the specialization
> 
>      template<> inline T::basetype MASK<T>(int width) ....

> template <> inline T::basetype
It looks like you're trying to partially specialize a function
template.  There is no such thing.

nathan

-- 
Nathan Sidwell    ::   http://www.codesourcery.com   ::     CodeSourcery LLC
nathan@codesourcery.com    ::     http://www.planetfall.pwp.blueyonder.co.uk


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: template function specialization trouble - probably a user error, but...
  2004-09-17 12:44 ` Nathan Sidwell
@ 2004-09-17 16:39   ` Alexandre Oliva
  2004-09-17 16:44     ` Nathan Sidwell
  0 siblings, 1 reply; 5+ messages in thread
From: Alexandre Oliva @ 2004-09-17 16:39 UTC (permalink / raw)
  To: Nathan Sidwell; +Cc: Alan Lehotsky, gcc

On Sep 17, 2004, Nathan Sidwell <nathan@codesourcery.com> wrote:

> Alan Lehotsky wrote:

>> template<> inline T::basetype MASK<T>(int width) ....

> It looks like you're trying to partially specialize a function
> template.  There is no such thing.

This is not partial specialization, it's explicit specialization,
which works for both class and function templates.  What he's missing
is a `typename' before T::basetype, such that GCC knows it's a
template-dependent type.

-- 
Alexandre Oliva             http://www.ic.unicamp.br/~oliva/
Red Hat Compiler Engineer   aoliva@{redhat.com, gcc.gnu.org}
Free Software Evangelist  oliva@{lsd.ic.unicamp.br, gnu.org}

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: template function specialization trouble - probably a user error, but...
  2004-09-17 16:39   ` Alexandre Oliva
@ 2004-09-17 16:44     ` Nathan Sidwell
  2004-09-18 17:33       ` Alexandre Oliva
  0 siblings, 1 reply; 5+ messages in thread
From: Nathan Sidwell @ 2004-09-17 16:44 UTC (permalink / raw)
  To: Alexandre Oliva; +Cc: Alan Lehotsky, gcc

Alexandre Oliva wrote:
> On Sep 17, 2004, Nathan Sidwell <nathan@codesourcery.com> wrote:
> 
> 
>>Alan Lehotsky wrote:
> 
> 
>>>template<> inline T::basetype MASK<T>(int width) ....
> 
> 
>>It looks like you're trying to partially specialize a function
>>template.  There is no such thing.
> 
> 
> This is not partial specialization, it's explicit specialization,
> which works for both class and function templates.  What he's missing
> is a `typename' before T::basetype, such that GCC knows it's a
> template-dependent type.
If its a specialization, then it can't have a dependent type.  Adding
typename won't fix things, as there's no 'T' in scope in the first place.

nathan

-- 
Nathan Sidwell    ::   http://www.codesourcery.com   ::     CodeSourcery LLC
nathan@codesourcery.com    ::     http://www.planetfall.pwp.blueyonder.co.uk


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: template function specialization trouble - probably a user error, but...
  2004-09-17 16:44     ` Nathan Sidwell
@ 2004-09-18 17:33       ` Alexandre Oliva
  0 siblings, 0 replies; 5+ messages in thread
From: Alexandre Oliva @ 2004-09-18 17:33 UTC (permalink / raw)
  To: Nathan Sidwell; +Cc: Alan Lehotsky, gcc

On Sep 17, 2004, Nathan Sidwell <nathan@codesourcery.com> wrote:

> If its a specialization, then it can't have a dependent type.  Adding
> typename won't fix things, as there's no 'T' in scope in the first place.

Doh.  Yes, indeed.  The testcase is terminally broken.

-- 
Alexandre Oliva             http://www.ic.unicamp.br/~oliva/
Red Hat Compiler Engineer   aoliva@{redhat.com, gcc.gnu.org}
Free Software Evangelist  oliva@{lsd.ic.unicamp.br, gnu.org}

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-09-18 16:52 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-09-17 12:27 template function specialization trouble - probably a user error, but Alan Lehotsky
2004-09-17 12:44 ` Nathan Sidwell
2004-09-17 16:39   ` Alexandre Oliva
2004-09-17 16:44     ` Nathan Sidwell
2004-09-18 17:33       ` Alexandre Oliva

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).