首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法将Persistent<Function>添加到vector<Persistent<Function>>

无法将Persistent<Function>添加到vector<Persistent<Function>>
EN

Stack Overflow用户
提问于 2016-01-11 19:11:26
回答 2查看 276关注 0票数 2

我正在从v8 3.x迁移到4.y,并且在我的一个头文件中定义的函数中我遇到了问题。

代码语言:javascript
运行
复制
//JavascriptBase.h
namespace Company {
    class Base {
      protected:
        void registerHandler(v8::Persistent<v8::Function>& func) {
            user_functions_.push_back(func);
        }
      private:
        std::vector<v8::Persistent<v8::Function>> user_functions_;
    }
}

当我试图编译时,我会得到以下错误:

代码语言:javascript
运行
复制
third-party2/v8/4.7.39/gcc-4.8.1-glibc-2.17-fb/281a9e6/include/v8.h:667:53: error: assigning to 'v8::Object *volatile' from incompatible type 'v8::Primitive *'
    while (false) { *(static_cast<O* volatile*>(0)) = static_cast<Primitive*>(0); };
                                                    ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~
third-party2/v8/4.7.39/gcc-4.8.1-glibc-2.17-fb/281a9e6/include/v8.h:663:5: note: in instantiation of function template specialization 'v8::NonCopyablePersistentTraits<v8::Function>::Uncompilable<v8::Object>' requested here
    Uncompilable<Object>();
    ^
third-party2/v8/4.7.39/gcc-4.8.1-glibc-2.17-fb/281a9e6/include/v8.h:7175:6: note: in instantiation of function template specialization 'v8::NonCopyablePersistentTraits<v8::Function>::Copy<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> >' requested here
  M::Copy(that, this);
     ^
third-party2/v8/4.7.39/gcc-4.8.1-glibc-2.17-fb/281a9e6/include/v8.h:729:5: note: in instantiation of function template specialization 'v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> >::Copy<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> >' requested here
    Copy(that);
    ^
third-party2/libgcc/4.8.1/gcc-4.8.1-glibc-2.17-fb/8aac7fc/include/c++/4.8.1/ext/new_allocator.h:120:23: note: in instantiation of member function 'v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> >::Persistent' requested here
 { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
                      ^
third-party2/libgcc/4.8.1/gcc-4.8.1-glibc-2.17-fb/8aac7fc/include/c++/4.8.1/bits/alloc_traits.h:254:8: note: in instantiation of function template specialization '__gnu_cxx::new_allocator<v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> > >::construct<v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> >, const v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> > &>' requested here
 { __a.construct(__p, std::forward<_Args>(__args)...); }
       ^
third-party2/libgcc/4.8.1/gcc-4.8.1-glibc-2.17-fb/8aac7fc/include/c++/4.8.1/bits/alloc_traits.h:393:4: note: in instantiation of function template specialization 'std::allocator_traits<std::allocator<v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> > > >::_S_construct<v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> >, const v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> > &>' requested here
 { _S_construct(__a, __p, std::forward<_Args>(__args)...); }
   ^
third-party2/libgcc/4.8.1/gcc-4.8.1-glibc-2.17-fb/8aac7fc/include/c++/4.8.1/bits/stl_vector.h:905:21: note: in instantiation of function template specialization 'std::allocator_traits<std::allocator<v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> > > >::construct<v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> >, const v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> > &>' requested here
     _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
                    ^
./Company/JavaScriptBase.h:84:21: note: in instantiation of member function 'std::vector<v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> >, std::allocator<v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> > > >::push_back' requested here
    user_functions_.push_back(func);
                    ^
1 error generated.

我已经做了一些搜索,发现Persistent对象是不可分配的,但是我不知道如何处理这些信息。任何帮助都将不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-11 19:31:13

如果它们是不可分配的,那么你(显然)就不能分配它们。而且,由于std::vector<>依赖于使用赋值运算符复制值,所以您的方法将无法工作。

但是,您可以存储指向它们的指针,因为复制任何东西的指针都是很好的定义。

票数 3
EN

Stack Overflow用户

发布于 2016-01-12 02:23:17

您可以使用这个“ty胡枝子”使一个可复制的持久

代码语言:javascript
运行
复制
using CopyablePersistent = v8::Persistent<T, v8::CopyablePersistentTraits<T>>;

这假设它在一个模板化的类中,但是你可以做你想要的T。

编辑:显然这是一件“非常糟糕”的事情,但我不知道为什么。

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

https://stackoverflow.com/questions/34729216

复制
相关文章

相似问题

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