By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,682 Members | 1,090 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,682 IT Pros & Developers. It's quick & easy.

SFINAE

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.