concept_check et SFINAE (edit : et C++0x aussi)
Salut à tous,
Je fais peu à peu mon deuil de la non inclusion des concepts dans C++0x en me penchant sur boost::concept_check, qui a finalement l'air intéressant.
Peut-on surcharger une fonction en utilisant concept_check et le mécanisme SFINAE ?
J'ai pensé que la simple utilisation de concept_check permettait ça (car tout est inclus dans la signature de la fonction), mais il semblerait que non :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#include <iostream>
#include <boost/concept_check.hpp>
#include <boost/concept/requires.hpp>
class integer
{
public:
integer(int value):
value_(value)
{
}
int
value() const
{
return value_;
}
private:
int value_;
};
bool
operator==(const integer& i, const integer& j)
{
return i.value() == j.value();
}
template<typename T>
class Comparable
{
public:
BOOST_CONCEPT_USAGE(Comparable)
{
t1 == t2;
}
private:
T t1;
T t2;
};
template<typename T>
class JavaStyleComparable
{
public:
BOOST_CONCEPT_USAGE(JavaStyleComparable)
{
t1.equals(t2);
}
private:
T t1;
T t2;
};
template<typename T>
BOOST_CONCEPT_REQUIRES
(
((Comparable<T>)),
(bool)
)
same_value(const T& t1, const T& t2)
{
return t1 == t2;
}
template<typename T>
BOOST_CONCEPT_REQUIRES
(
((JavaStyleComparable<T>)),
(bool)
)
same_value(const T& t1, const T& t2)
{
return t1.equals(t2);
}
int
main()
{
integer i(0);
integer j(0);
if(same_value(i, j))
{
std::cout << "woohoo!\n";
}
else
{
std::cout << "d'oh!\n";
}
return 0;
} |
Code:
1 2 3 4 5 6 7 8
| main.cpp|85| error: call of overloaded same_value(integer&, integer&) is ambiguous
main.cpp|63| note: candidates are: typename boost::Requires_<(0 + boost::_requires_::value), void (*)(bool)>::type same_value(const T&, const T&) [with T = integer]
main.cpp|74| note: typename boost::Requires_<(0 + boost::_requires_::value), void (*)(bool)>::type same_value(const T&, const T&) [with T = integer]
|| main.cpp: In destructor JavaStyleComparable<T>::~JavaStyleComparable() [with T = integer]:
/usr/include/boost/concept/detail/general.hpp|29| instantiated from static void boost::concept::requirement<Model>::failed() [with Model = JavaStyleComparable<integer>]
/usr/include/boost/concept/requires.hpp|31| instantiated from boost::_requires_<void (*)(JavaStyleComparable<integer>)>
main.cpp|85| instantiated from here
main.cpp|49| error: class integer has no member named equals |
J'ai parcouru la doc et je n'ai pas vu un tel cas d'utilisation. On peut faire quelque chose en ajoutant du enable_if ? Si oui comment ?