首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >scoped_allocator_adaptor似乎要求默认构造分配器

scoped_allocator_adaptor似乎要求默认构造分配器
EN

Stack Overflow用户
提问于 2020-12-25 07:45:31
回答 1查看 54关注 0票数 0

在我的scoped_allocator_adaptor实验中,我试图传递从main(..)获得的分配器。到S1的构造函数中(更一般地,在S1中会有多个不同的类型,它们都使用构造函数中提供的分配器)。然而,我得到了下面的编译错误,表明分配器应该是默认的可构造的。有人能解释一下为什么会这样吗?是否发生了导致所需分配器的缺省构造版本的转换?

代码语言:javascript
复制
#include <iostream>
#include <cassert>
#include <vector>
#include <scoped_allocator>

// Move allocator and container aliases into namepsace 
namespace custom
{
    template <typename T>
    struct MyAlloc
    {
        using value_type = T;
        MyAlloc(const std::string &scope) noexcept : _scope(scope)  {} 

        // Rebinding allocatos to different type 
        template <class U> 
        MyAlloc(const MyAlloc<U> & other) noexcept : _scope(other._scope)  {}

        // Allow for move operations to be noexcept
        //using is_always_equal = std::true_type;

        value_type*  allocate(std::size_t n) noexcept
        {
            std::cout << "Allocating " << n << " objects within " << _scope << " from " << __PRETTY_FUNCTION__ << std::endl;
            return static_cast<value_type*>(::operator new (n*sizeof(value_type)));
        }
        void deallocate(value_type* p, std::size_t n) noexcept
        {
            std::cout << "Deallocating " << n << " objects within " << _scope << " from " << __PRETTY_FUNCTION__ << std::endl;
            ::operator delete(p);
        }
        std::string _scope;
    };

    // Allocators compare equal to enable one allocator to de-allocate memory
    // from another
    template <typename T>
    bool operator==(const MyAlloc<T> &x1, const MyAlloc<T> &x2) noexcept
    {
        return true;
    }

    template <typename T>
    bool operator!=(const MyAlloc<T> &x1, const MyAlloc<T> &x2) noexcept
    {
        return !(x1 == x2);
    }

    template <typename T>
    using allocator = std::scoped_allocator_adaptor<MyAlloc<T>>;

    template <typename T> //  adaptor to propagate
    using vector = std::vector<T, allocator<T>>;

    template <typename T> 
    using bstr = std::basic_string<T, std::char_traits<T>, allocator<T>>;
    using string = bstr<char>;
}

struct S1
{
   using allocator_type = custom::allocator<std::byte>;
   S1(allocator_type alloc) : str("This is a very long string indeed..", std::allocator_traits<allocator_type>::rebind_alloc<char>(alloc))
   {
      std::cout << __PRETTY_FUNCTION__ << std::endl;
   }
   S1(const S1 &other, allocator_type al)  : str(other.str, std::allocator_traits<allocator_type>::rebind_alloc<char>(al))
   {
      std::cout << __PRETTY_FUNCTION__ << std::endl;
   }
   custom::string str;
};



int main()
{
   custom::allocator<std::byte> sc{"scope"};
   custom::vector<S1> cv{sc};
//   cv.emplace_back();
}

编译错误:

代码语言:javascript
复制
/usr/include/c++/10/scoped_allocator: In instantiation of ‘std::scoped_allocator_adaptor<_OuterAlloc, _InnerAllocs>::scoped_allocator_adaptor() [with _OuterAlloc = custom::MyAlloc<S1>; _InnerAllocs = {}]’:
/usr/include/c++/10/bits/stl_vector.h:626:35:   required from here
/usr/include/c++/10/scoped_allocator:304:60: error: no matching function for call to ‘custom::MyAlloc<S1>::MyAlloc()’
  304 |       scoped_allocator_adaptor() : _OuterAlloc(), _M_inner() { }
      |                                                            ^
d2.cc:17:9: note: candidate: ‘template<class U> custom::MyAlloc<T>::MyAlloc(const custom::MyAlloc<U>&) [with U = U; T = S1]’
   17 |         MyAlloc(const MyAlloc<U> & other) noexcept : _scope(other._scope)  {}
      |         ^~~~~~~
d2.cc:17:9: note:   template argument deduction/substitution failed:
In file included from d2.cc:4:
/usr/include/c++/10/scoped_allocator:304:60: note:   candidate expects 1 argument, 0 provided
  304 |       scoped_allocator_adaptor() : _OuterAlloc(), _M_inner() { }
      |                                                            ^
d2.cc:13:9: note: candidate: ‘custom::MyAlloc<T>::MyAlloc(const string&) [with T = S1; std::string = std::__cxx11::basic_string<char>]’
   13 |         MyAlloc(const std::string &scope) noexcept : _scope(scope)  {}
      |         ^~~~~~~
d2.cc:13:9: note:   candidate expects 1 argument, 0 provided
d2.cc:10:12: note: candidate: ‘custom::MyAlloc<S1>::MyAlloc(const custom::MyAlloc<S1>&)’
   10 |     struct MyAlloc
      |            ^~~~~~~
d2.cc:10:12: note:   candidate expects 1 argument, 0 provided
d2.cc:10:12: note: candidate: ‘custom::MyAlloc<S1>::MyAlloc(custom::MyAlloc<S1>&&)’
d2.cc:10:12: note:   candidate expects 1 argument, 0 provided
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-25 08:53:16

这一行:custom::vector<S1> cv{sc};是问题所在。

因为您已经使用了方括号,所以它试图调用vector的初始化器列表构造函数,该构造函数有一个可选参数,该参数是它默认构造的一个分配器。

请参阅the list on cppreference中的最后两个构造函数

如果将该行更改为custom::vector<S1> cv(sc);,它将编译,但不会出现错误。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65444125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档