Poniższy kod nie kompiluje:boost :: wariant i przeciążenie funkcja rozdzielczości
#include <boost/variant.hpp>
class A {};
class B {};
class C {};
class D {};
using v1 = boost::variant<A, B>;
using v2 = boost::variant<C, D>;
int f(v1 const&) {
return 0;
}
int f(v2 const&) {
return 1;
}
int main() {
return f(A{});
}
zarówno gcc i brzęk narzeka:
test1.cpp: In function ‘int main()’:
test1.cpp:18:17: error: call of overloaded ‘f(A)’ is ambiguous
return f(A{});
^
test1.cpp:18:17: note: candidates are:
test1.cpp:11:5: note: int f(const v1&)
int f(v1 const&) {
^
test1.cpp:14:5: note: int f(const v2&)
int f(v2 const&) {
Biorąc pod uwagę, że nie jest możliwe skonstruowanie v2
obiektu od instancji A
, dlaczego kompilator nadaje ten sam priorytet obu funkcjom w czasie działania zabezpieczenia przed przeciążeniem?
'variant' ma domyślne konstruktory konwertujące szablony. Jeśli nie są one odpowiednio zabezpieczone przez 'enable_if' może to być wynik. – pmr