我試圖編譯這個例子中,其中可變參數類模板從鹼基的可變參數量繼承,其中的每一個實現了不同operator[]
:曖昧操作符[]中可變參數模板
#include <iostream>
template <typename T>
struct Field {
typename T::value_type storage;
typename T::value_type &operator[](const T &c) {
return storage;
}
};
template<typename... Fields>
struct ctmap : public Field<Fields>... {
};
int main() {
struct age { typedef int value_type; };
struct last_name { typedef std::string value_type; };
ctmap<last_name, age> person;
person[last_name()] = "Smith";
person[age()] = 104;
std::cout << "Hello World!" << std::endl;
return 0;
}
當我編譯與海灣合作委員會(Debian的4.9.2-10),我得到以下錯誤
main.cpp: In function ‘int main()’:
main.cpp:22:23: error: request for member ‘operator[]’ is ambiguous
person[last_name()] = "Smith";
^
main.cpp:7:27: note: candidates are: typename T::value_type& Field<T>::operator[](const T&) [with T = main()::age; typename T::value_type = int]
typename T::value_type &operator[](const T &c) {
^
main.cpp:7:27: note: typename T::value_type& Field<T>::operator[](const T&) [with T = main()::last_name; typename T::value_type = std::basic_string<char>]
main.cpp:23:17: error: request for member ‘operator[]’ is ambiguous
person[age()] = 104;
^
main.cpp:7:27: note: candidates are: typename T::value_type& Field<T>::operator[](const T&) [with T = main()::age; typename T::value_type = int]
typename T::value_type &operator[](const T &c) {
^
main.cpp:7:27: note: typename T::value_type& Field<T>::operator[](const T&) [with T = main()::last_name; typename T::value_type = std::basic_string<char>]
爲什麼這種含糊?
這是由鐺接受++ 3.8:http://melpon.org/wandbox/permlink/huzwGp0kc2OafMZl(但我在這種情況下,我不確定哪個編譯器是正確的) – dyp
(基本上,多個基類中的成員函數不會超載,但我不確定它如何與運算符查找相互作用;乍一看似乎是錯誤的) – dyp
你可以使它符合線性/ t基於ree的繼承和'使用'聲明。 – Yakk