473,320 Members | 2,024 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,320 software developers and data experts.

SFINAE

Is it possible to use SFINAE to provide different implementations of
some function depending on the fact that operator << is overloaded for
some type?

For example:

template<class T>
void output1(const T &t)
{
// This implementation should be in effect for types that support
<<
std::cout << t;
}

template<class T>
void output2(const T &t)
{
// This implementation should be in effect for types that do not
support <<
my_output(t);
}

So that this usage is possible:

T t;
output(t); // uses output1 or output2 depending on << operator
defined for T

Apr 26 '06 #1
4 3161
ka****@gmail.com wrote:
Is it possible to use SFINAE to provide different implementations of
some function depending on the fact that operator << is overloaded for
some type?
I have no idea what a "SFINAE" is.
For example:

template<class T>
void output1(const T &t)
{
// This implementation should be in effect for types that support
<<
std::cout << t;
}

template<class T>
void output2(const T &t)
{
// This implementation should be in effect for types that do not
support <<
my_output(t);
}

So that this usage is possible:

T t;
output(t); // uses output1 or output2 depending on << operator
defined for T


You could use some kind of traits class.

Apr 26 '06 #2
Rolf Magnus wrote:
ka****@gmail.com wrote:
Is it possible to use SFINAE to provide different implementations of
some function depending on the fact that operator << is overloaded
for some type?
I have no idea what a "SFINAE" is.


REALLY?!! OK, I'll be the first to tell you. It stands for "Substitution
Failure Is Not An Error". Look it up on the Web. It's a technique you can
use to play some interesting tricks using templates.
[...]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 26 '06 #3

ka****@gmail.com wrote:
Is it possible to use SFINAE to provide different implementations of
some function depending on the fact that operator << is overloaded for
some type?
It should be: what is your problem?
For example:

template<class T>
void output1(const T &t)
{
// This implementation should be in effect for types that support
<<
std::cout << t;
}

template<class T>
void output2(const T &t)
{
// This implementation should be in effect for types that do not
support <<
my_output(t);
}

So that this usage is possible:

T t;
output(t); // uses output1 or output2 depending on << operator
defined for T


I do not really see the reason to do it with SFINAE (Substitution
Failure Is Not An Error). The most obvious way to do solve your problem
would be to provide operator<< for those classes. If that is not
appropriate, I believe I would simply provide:

template <typename T>
void my_output(T const& t) { std::cout << t; }

/Peter

Apr 26 '06 #4
ka****@gmail.com wrote:
Is it possible to use SFINAE to provide different implementations of
some function depending on the fact that operator << is overloaded for
some type?

For example:

template<class T>
void output1(const T &t)
{
// This implementation should be in effect for types that support
<<
std::cout << t;
}

template<class T>
void output2(const T &t)
{
// This implementation should be in effect for types that do not
support <<
my_output(t);
}

So that this usage is possible:

T t;
output(t); // uses output1 or output2 depending on << operator
defined for T


Why not create a container class template something like as follows:

template <typename T>
class My_Output
{
private:
const T &val:
public:
My_Output(const T &val) : val(inp)
{
}
friend std::ostream &operator<<(std::ostream &str, const MyOutput
&inp);
};

template <typename T>
std::ostream &operator<<(std::ostream &str, const MyOutput<T> &inp)
{
// Add your code here

return str;
}

MyClass special;
// Your code here
std::cout << My_Output(special) << std::endl;

Hope this helps.

JB
Apr 26 '06 #5

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...
6
by: edd | last post by:
Hello all, Is there a way to determine whether a particular type supports the -> operator at compile time? I'm trying to write a template function (or a series of overloads) that will yield the...
3
by: none | last post by:
I am trying to understand SFINAE based on this wiki page: http://en.wikipedia.org/wiki/SFINAE // Defines return type to be void for all types // besides int. template<typename T> struct...
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:
0
by: greek_bill | last post by:
Hi, I have a template function for which I use SFINAE to restrict one of the parameters. Then I also have a partial specialization of this function.I would like to provide an explicit...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.