這是一個對象的我(剝離)類和實例:爲什麼這個構造函數的重載不能正確解析?
template <typename T, typename Allocator = std::allocator<T> >
class Carray {
typedef typename Allocator::size_type size_type;
// ...
explicit Carray(size_type n, const T& value, const Allocator& alloc = Allocator()) {
// ...
}
template<typename InputIterator>
Carray(InputIterator first, InputIterator last, const Allocator& alloc = Allocator()) {
// ...
}
// ...
}
Carray<int> array(5, 10);
我希望它可以調用Carray(size_type, const T&, const Allocator&)
構造,但事實並非如此。大致上這決定到template<typename InputIterator> Carray(InputIterator, InputIterator, const Allocator&)
。
我該如何改變以使其按預期工作?我覺得它也很奇怪,因爲打電話給std::vector<int> v(5, 10)
工作得很好。如果我看的構造函數的定義,在我的GCC的實現,我覺得這(我改名一些編譯器實現的名字,像__n
):
template<typename T, typename A = std::allocator<T> >
class vector {
typedef size_t size_type;
typedef T value_type;
typedef A allocator_type;
// ...
explicit vector(size_type n, const value_type& value = value_type(), const allocator_type& a = allocator_type());
template<typename InputIterator>
vector(InputIterator first, InputIterator last, const allocator_type& a = allocator_type());
// ...
};
這似乎是相同的。
謝謝,這允許我完全定義我的頭文件,而不依賴於boost或非C++ 03。我當然不會在生產代碼中使用它(boost :: enable_if更容易使用和適用)。 – orlp 2011-05-18 22:28:15
@ nightcracker,不用擔心......這是一個有趣的挑戰...... – Nim 2011-05-18 22:32:13