Template specialization problem - compiler bug or my fault?

I

Imre

Hi

I have some non-compiling code, and I'd like to know if it's a compiler
bug, or something I'm doing wrong.

In the following code (it's a simplified version of the real code where
I met the problem) the call to F<int> instantiates S<int>. I think the
compiler should choose the specialized version of S<>, but it seems to
use the primary template instead, leading to an invalid non-lvalue int
-> int& conversion.

Strangely, if I explicitly instantiate S<int> (uncomment the commented
line), then it compiles fine.

I also tried implementing enable_if and is_fundamental myself, and came
to the exact same results.

Also tested that version (with the custom enable_if and is_fundamental)
with the online Comeau compiler, and it compiled fine.

So, I'd like to know whether this stuff should work according to the
standard, and if not, why; and if anyone could suggest a workaround
that works with VC++ 7.1.
Thanks,

Imre

Here's the code:

#include <boost/type_traits.hpp>
#include <boost/type_traits/is_fundamental.hpp>
#include <boost/utility/enable_if.hpp>

template <typename T, typename Enable = void>
struct S
{
typedef T& type;
};

template <typename T>
struct S<T, typename
boost::enable_if_c<boost::is_fundamental<T>::value>::type>
{
typedef T type;
};

//template struct S<int>; // Compiles fine with VC if this line is
uncommented

template <typename T>
typename S<T>::type F()
{
return 0;
}

int main(int argc, char* argv[])
{
F<int>();
return 0;
}
 

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,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top