Re: Free Function Detection
  Home FAQ Contact Sign in
comp.lang.c++.moderated only
 
Advanced search
POPULAR GROUPS

more...

 Up
Re: Free Function Detection         

Group: comp.lang.c++.moderated · Group Profile
Author: Roman.Perepelitsa
Date: Apr 3, 2008 13:48

On 3 Apr, 17:13, "Nevin :-] Liber" eviloverlord.com> wrote:
> Is it possible to detect at compile time if, for a given user defined
> type UDT, that I can make the following function call:
>
> operator<<(std::ostream&, UDT const&)
>
> Basically, I want to call it when I can and do something else when I
> can't.

This implementation is based on boost/detail/is_incrementable.hpp
You can find a lot of comments in this file.

#include

namespace detail
{
struct tag {};

struct any { template any(T const&); };

tag operator<<(std::ostream&, any const&);
tag operator,(tag,int);

char (& check(tag) )[2];

template
char check(T const&);

template
struct impl
{
static T const& x;
static std::ostream& strm;
static const bool value = sizeof(check((strm << x,0))) == 1;
};
}

template
struct is_ostreamable : detail::impl {};

// Test

#include
#include

struct foo {};
struct bar {};

std::ostream & operator<<(std::ostream &, foo const&);

int main()
{
std::cout << is_ostreamable::value << std::endl; // 1
std::cout << is_ostreamable::value << std::endl; // 1
std::cout << is_ostreamable::value << std::endl; // 0
}

HTH,
Roman Perepelitsa.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
no comments
diggit! del.icio.us! reddit!

RELATED THREADS
SubjectArticles qty Group
Hypergeometric functions and beta functionssci.math ·