473,473 Members | 2,255 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

restrict_to, enable_if, etc

With regards to using Boost enable_if and similar SFINAE constructs,
is it right that you can only put an enable_if on the return type or
as an additional parameter? Why is that?

The following doesn't compile (VC8 and gcc 3.4.4) :

namespace Test
{

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

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

//------------------------------------------------------------------

struct Foo
{
// Doesn't compile
template<class T>
void Func(typename enable_if_c<!IsSameType<T, int>::value, T>::type
t)
{}

// But this does...
template<class T>
typename enable_if_c<!IsSameType<T, int>::value, void>::type Func(T
t)
{}

void Func(int i)
{}
};

void FooFunc()
{
Foo f;

// Should go to non-template version
f.Func(5);

// Should go to template version
char* p;
f.Func(p);
}

}
Thanks,

Vassilis
Oct 6 '08 #1
3 2713
greek_bill wrote:
With regards to using Boost enable_if and similar SFINAE constructs,
is it right that you can only put an enable_if on the return type or
as an additional parameter? Why is that?

The following doesn't compile (VC8 and gcc 3.4.4) :

namespace Test
{

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

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

//------------------------------------------------------------------

struct Foo
{
// Doesn't compile
template<class T>
void Func(typename enable_if_c<!IsSameType<T, int>::value, T>::type
t)
{}

// But this does...
template<class T>
typename enable_if_c<!IsSameType<T, int>::value, void>::type Func(T
t)
{}

void Func(int i)
{}
};
[more code snipped]

This is really odd. Please, copy-and-paste the exact code you are trying
to compile (the above can't be it, of course).

--
Gennaro Prota | name.surname yahoo.com
Breeze C++ (preview): <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I'm available.
Oct 6 '08 #2
while copying and pasting I missed out the definition of enable_if_c,
which I have it as :

template<bool b, class T>
struct enable_if_c
{
typedef T type;
};

template<class T>
struct enable_if_c<false, T{};

but other than that it the above code should be enough to illustrate
the problem. You need to comment out one or the other template
implementation of Func().
Oct 7 '08 #3
greek_bill wrote:
You need to comment out one or the other template
implementation of Func().
That's the info you should have given :-)

About your original question, you *can* have one function parameter and
enable_if on it (its type), but you'll usually face another issue:
something like

foo< ... >::

is a so-called "non-deduced context". A "classical" example is:

template< typename T >
struct identity
{
typedef T type ;
} ;

template< typename T >
void
f( typename identity< T >::type t )
{
}

int main()
{
f( 1 ) ; // can't deduce
}

So, even if you had simply

struct Foo
{
template< typename T >
void Func( typename enable_if_c< true, T >::type )
{
}
} ;

it wouldn't work for your p (FWIW, f.Func< char * >( p ) would,
instead). On the contrary, your second template gives a way to deduce an
argument for T; and, after all arguments are deduced, they are
substituted in non-deduced contexts (later, SFINAE applies).

--
Gennaro Prota | name.surname yahoo.com
Breeze C++ (preview): <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I'm available.
Oct 8 '08 #4

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

Similar topics

13
by: al | last post by:
1. If a function template is defined: template <typename A> A SQ(A& a) { return a*a; } How could prevent users from passing a type which makes no sense, like Employee class type?
5
by: Mohammad | last post by:
Hi, Is it possible to disable a method of a template class depending on the typename at compile time? thanks!
5
by: John Harrison | last post by:
I've tried the following program on two different compilers (VC++ 7.1 and gcc 3.3.1) and both print "first" when I expected "second". Why is that? #include <vector> #include <iostream> ...
3
by: Boris | last post by:
Hello, I have written a program that compiles just fine with GCC 3.3. With GCC 3.4.1 it results in this error: g++ -O0 -g3 -Wall -c -oTestp.o ../Testp.c .../Testp.c: In function `int main()':...
4
by: Imre | last post by:
Why is the function template a better match for the call in the following code? And how could I write a version of F() that should be called if the argument is a pointer to a type that is...
3
by: Ruben Campos | last post by:
Greetings. Some time ago, I had writing a CVector <T, N> class, which implements an algebraic vector of arbitrary both dimension and scalar type. First, I defined the interface for the generic...
4
by: Dan Krantz | last post by:
I have the following template to ensure that a given number (val) falls into a range (between vmin & vmax): template<typename T> T ForceNumericRange( const T& val, const T& vmin, const T& vmax)...
3
by: Christoph Bartoschek | last post by:
Hi, I try to use boost::enable_if inside of a constructor but the following code fails. How can I correct it? #include <boost/utility/enable_if.hpp> #include...
2
by: Philipp Reh | last post by:
Dear group, I recently tried to port some of my code to a VC++8.0 environment. I noticed that some boost::enable_if related code fails to compile there which works under gcc. I've made a...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.