samedi 28 mars 2015

Checking whether a function (not a method) exists in c++11 via templates

So with SFINAE and c++11, it is possible to implement two different template functions based on whether one of the template parameters can be substituted.


For example



struct Boo{
void saySomething(){ cout << "Boo!" << endl; }
};

template<class X>
void makeitdosomething(decltype(&X::saySomething), X x){
x.saySomething();
}

template<class X>
void makeitsaysomething(int whatever, X x){
cout << "It can't say anything!" << endl;
}


int main(){
makeitsaysomething(3);
makeitsaysomething(Boo());
}


or something along that line.


My question is.. how does one do the same thing, but for non-member functions?


In particular I'm trying to check if there's such thing as an:



operator<<(std::ostream& os, X& whateverclass);


that exists. Is it possible to test it?


Aucun commentaire:

Enregistrer un commentaire