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

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