473,409 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

SFINAE and explicit instantiation

Hi,

I have a template function for which I use SFINAE to restrict one of
the parameters. Then I also have a partial specialization[1] of this
function.I would like to provide an explicit instantiation of the
partially specialized version, but my compiler (VC8) complains because
it fails the SFINAE version.

[1] I just realized as I was typing this that I'm using partial
_function_ specialization. I'm sure I remember reading somewhere that
this isn't allowed. VC8 and gcc are quite happy with it though.
Anyway...

Code as follows :

// Used for the SFINAE condition

template<typename T, typename U>
struct IsSameType { enum { value = false }; };

template<typename T>
struct IsSameType<T,T{ enum { value = true }; };
// SFINAE helper

template<bool allow, class T>
struct RestrictTo {};

template<class T>
struct RestrictTo<true, T{ typedef T type; };
// SFINAE function template

class Foo {};

template<class A, class T>
typename RestrictTo<IsSameType<T,int>::value, void>::type
Func(A, T)
{}

// Partially specialized version
template<class A>
void Func(A, float)
{}

// Explicit instantiation
template void Func(float, float);
This compiles fine with g++. VC8 says :

error C2794: 'type' : is not a member of any direct or indirect base
class of 'RestrictTo<allow,T>'
with
[
allow=false,
T=void
]
As I understand it, if I was to just call Func(1.0f, 2.0f) then SFINAE
would kick in an reject the first version, leading to the specialized
version to be picked up. If I didn't have SFINAE then there would be
an ambiguity.

It seems though that this rule isn't follow if I try to do an explicit
instantiation. It looks like the compiler thinks that I'm trying to
explicitly instantiate the SFINAE version with an argument that
violates the condition I specified.

Interestingly, qualifying the explicit specialization with <float>,
i.e.

template void Func<float>(float, float);
doesn' t help matters at all. I was hoping that if I supply <float>
that would be enough of a hint to the compiler that I'm trying to
instantiate a template with just one parameter.
gcc 4.3.2 compiles this fine by the way.
Thanks,

Bill

Nov 21 '08 #1
0 2870

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Peter Collingbourne | last post by:
Hello I am trying to do some template metaprogramming involving enabling or disabling a method in a parameterised class based on some condition. I am trying to use the Boost enable_if mechanism...
3
by: justin.adam.miller | last post by:
I've been trying to use the sfinae principle in some code and have been getting many compiler errors. So I decided to try a very simplified version to see if I had the idea correct. Here's the...
2
by: Clark S. Cox III | last post by:
I'm writing a class that, depending on a template parameter, has constructors that take differing numbers of arguments. I initially thought that I could use SFINAE (via boost::enable_if_c) to...
1
by: Dilip | last post by:
I am a little confused about the difference between SFINAE and unambiguous overload resolution set. For ex: template<typename Tvoid func(T); template<typename Tvoid func(T*); now, func<intis...
4
by: Gianni Mariani | last post by:
Below is an attempt to factorize the "SFINAE" idiom. I have tried it on three compilers and they all complain. As far as I can tell, it's valid code. The questions are, are the compilers right...
5
by: Fei Liu | last post by:
Hello, I just hit a strange problem regarding SFINAE. The following code causes compile error (void cannot be array element type), I thought SFINA should match test(...) version instead and not...
2
by: Barry | last post by:
The problem brought by one earlier post from comp.lang.c++ http://groups.google.com/group/comp.lang.c++/browse_thread/thread/bf636c48b84957b/ I take part of the question and reproduce the code to...
35
by: James Kanze | last post by:
Just ran into an interesting question concerning SFINAE. Given the following code: #include <iostream> #include <typeinfo> template< typename T > class P { public:
11
by: Hendrik Schober | last post by:
Hi, I'm having two overloaded function templates, #include <iterator> template< typename T > void test( T /*a1*/, T /*a2*/ ) {} template< typename Iter >
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.