This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

template function specialization trouble - probably a user error, but...


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]