orl*_*rlp 5 c++ constructor class overload-resolution
这是我的(剥离)类和一个对象的实例化:
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);
Run Code Online (Sandbox Code Playgroud)
我希望这可以调用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());
// ...
};
Run Code Online (Sandbox Code Playgroud)
这似乎是一样的.
显式构造函数需要size_t和int.你提供了两个整数.
代int用于InputIterator使模板更好的匹配.
如果仔细观察标准容器,您将看到它们使用一些模板元编程来确定InputIterator它是否是真正的迭代器或者它是否是整数类型.然后重定向到不同的结构.
编辑
这是一种方法:
template<class _InputIterator>
vector(_InputIterator _First, _InputIterator _Last,
const allocator_type& _Allocator = allocator_type() )
: _MyAllocator(_Allocator), _MyBuffer(nullptr), _MySize(0), _MyCapacity(0)
{ _Construct(_First, _Last, typename std::is_integral<_InputIterator>::type()); }
private:
template<class _IntegralT>
void _Construct(_IntegralT _Count, _IntegralT _Value, std::true_type /* is_integral */)
{ _ConstructByCount(static_cast<size_type>(_Count), _Value); }
template<class _IteratorT>
void _Construct(_IteratorT _First, _IteratorT _Last, std::false_type /* !is_integral */)
{ _Construct(_First, _Last, typename std::iterator_traits<_IteratorT>::iterator_category()); }
Run Code Online (Sandbox Code Playgroud)
如果编译器没有std :: type_traits,你也可以使用boost :: type_traits.
这应该适用于所有迭代器类型(包括指针)和当前标准。
#include <iostream>
#include <iterator>
#include <vector>
// uses sfinae to determine if the passed in type is indeed an iterator
template <typename T>
struct is_iterator_impl
{
typedef char yes[1];
typedef char no[2];
template <typename C>
static yes& _test(typename C::iterator_category*);
template <typename>
static no& _test(...);
static const bool value = sizeof(_test<T>(0)) == sizeof(yes);
};
template <typename T, bool check = is_iterator_impl<T>::value>
struct is_iterator
{
typedef void type;
};
template <typename T>
struct is_iterator<T, false>
{
};
template <typename T>
struct is_iterator<T*, false>
{
typedef void type;
};
template <typename T>
struct foo
{
explicit foo(std::size_t n, const T& value)
{
std::cout << "foo:size_t" << std::endl;
}
template<typename InputIterator>
foo(InputIterator first, InputIterator last, typename is_iterator<InputIterator>::type* dummy = 0)
{
std::cout << "foo::iterator" << std::endl;
}
};
int main(void)
{
// should not cause a problem
foo<int> f(1, 2);
// using iterators is okay
typedef std::vector<int> vec;
vec v;
foo<int> b(v.begin(), v.end());
// using raw pointers - is okay
char bar[] = {'a', 'b', 'c'};
foo<char> c(bar, bar + sizeof(bar));
}
Run Code Online (Sandbox Code Playgroud)
说明,迭代器通常必须定义多种类型,例如iterator_category,并且您可以利用 this 和 sfinae 来检测真正的迭代器。复杂的是,指针也是迭代器,但没有定义这些类型(某些东西std::iterator_traits提供了专门化),因此上面采用类似的方法,如果传入的类型是指针,则默认情况下将其视为迭代器。这种方法使您不必测试整数类型。
请参阅演示:http://www.ideone.com/E9l1T