C++ 놀이터에서 컨테이너를 가지고 놀고 있는데 기술적 인 문제가 있습니다.템플릿 멤버 함수 및 인수 전달
컨테이너에 emplace 메소드를 구현하려고합니다. 지금은 이미 구성한 요소를 가져 와서 할당 자 생성 메서드에 전달해야합니다.
나는 emplaceA
템플릿과 emplaceB1
, emplaceB2
템플릿의 세 가지 방법으로 끝냈습니다. 모두 의도 한대로 작동합니다.
내 문제는 emplaceA
은 Arg
이 단지 T
일 수 있음을 명시 적으로 나타내지 않습니다. emplaceB1
, emplaceB2
은 두 곳의 서로 다른 위치에서 거의 동일한 구현을 제공합니다 (결함으로 보입니다).
해결 방법이 있습니까?
template<class T, class A> class container {
public:
using allocator_traits = typename std::allocator_traits<A>;
...
template<class Arg> void emplaceA (int n, Arg&& arg){
allocator_traits::construct(allocator_, data_+n, std::forward<Arg>(arg));};
void emplaceB1(int n, const T& t){
allocator_traits::construct(allocator_, data_+n, t);};
void emplaceB2(int n, T&& t){
allocator_traits::construct(allocator_, data_+n, std::move(t));};
...
};
C++에는 함수 오버로드가 있습니다. 즉, 같은 이름이지만 다른 인수를 가진 두 개 이상의 함수를 가질 수 있습니다. –
이것은 아주 이상한 emplace 구현입니다. – SergeyA
이것은'emplace'보다'push_back'처럼 보입니다. 이렇게하면'emplaceB1'과'emplaceB2'에 대해 기분이 나아질 경우 [std :: vector :: push_back'] (http://en.cppreference.com/w/cpp/container/vector/push_back)도 제공됩니다 같은 두 가지 맛. –