提升::变体; std::unique_ptr 和复制

2023-11-25

这个问题确定了不可复制类型不能与Boost变体一起使用

Tree class

template <class T = int>

class Tree{

private:

         class TreeNode{

         public:
                 std::unique_ptr Nodes
                 Move constructors and move assignment + other public members

         private:

                 TreeNode(const TreeNode &other);      (= delete not supported on compiler)
                 TreeNode& operator=(const TreeNode &rhs);    (= delete not supported on compiler)


         };  // End Tree Node Class Definition


         Tree(const Tree &other);     (= delete not supported on compiler)
         Tree& operator=(const Tree &rhs);    (= delete not supported on compiler)

public:

         Move constructors and move assignment + other public members
};

TreeVisitor class

class TreeVisitor : public boost::static_visitor<bool> {
public:
        TreeVisitor() {}

        bool operator() (BinarySearchTree<std::string>& tree) const {
            return searchTree.load(tree);
        }
private:

};

TreeVariant

typedef boost::variant<Tree<std::string>, Tree<int>> TreeVariant;     
TreeVariant tree;

Tree<std::string> stringTree;
Tree<int> intTree;

正在申请Visitors如下

tree = intSearchTree;
boost::apply_visitor(TreeVisitor(), tree)

还使用 boost::bind 来获取所需的参数

boost::bind(TreeVisitor(), tree, val, keyIndex);

类型的编译器错误

error C2248: 'Tree<T>::Tree' : cannot access private member declared in class 'Tree<T>'  <----- related to private copy constructor in Tree (not TreeNode)
tree = stringTree;  <-------  error related to assignment

Tree编译正确并且已经过测试。如何解决这些与尝试获取副本相关的编译错误Tree类,因为std::unique_ptr,是不可能的吗?

SSCCE

<class T = int>

class Tree{

private:

class TreeNode{

public:

    TreeNode() {}
    ~TreeNode() {}  

    TreeNode(TreeNode &&other) : 
        key(other.key), index(other.index), left(std::move(other.left)), right(std::move(other.right)) 
    {
        key = index = left = right = nullptr; 
    }

    TreeNode &operator=(BTreeNode &&rhs)
    { 
        if(this != &rhs) 
        { 
            key = rhs.key; index = rhs.index; 
            left = std::move(rhs.left); right = std::move(rhs.right); 
            rhs.key = rhs.index = rhs.left = rhs.right = nullptr;
        } 
        return *this;
    }

    TreeNode(const T &new_key, const T &new_index) :
        key(new_key), index(new_index), left(nullptr), right(nullptr) {}

    friend class Tree;

private:

    TreeNode(const BinarySearchTreeNode &other);
    TreeNode& operator=(const BinarySearchTreeNode &rhs);

    std::unique_ptr<TreeNode> left;
    std::unique_ptr<TreeNode> right;

};  // End Tree Node Class Definition

std::unique_ptr<TreeNode> root;

BinarySearchTree(const BinarySearchTree &other);
BinarySearchTree& operator=(const BinarySearchTree &rhs);


public:

Tree() : root(nullptr), flag(false), run(true), leftCount(0), rightCount(0) {}

~Tree() {}

Tree(BinarySearchTree &&other) : root(std::move(other.root)) { other.root = nullptr; }

Tree &operator=(BinarySearchTree &&rhs) 
{ 
    if(this != &rhs)
    { 
        root = std::move(rhs.root); 
        rhs.root = nullptr;
    } 
    return *this;
}


};

使用示例:

bool delete_(){

    while(!instances.empty()){
                    // grab first instance
                    keyIndex = instances.at(0);
                    // compute end of the tuple to delete
                    endIndex = keyIndex + sizeToDelete;

                    // read the first attribute
                    try{
                        temp = boost::trim_copy(dataFile->readData(keyIndex, domainSize));
                    }
                    catch (std::exception &e){
                        printw("Error reading from the data file");
                    }

                    // delete tuple from data file
                    if(!dataFile->deleteTuple(keyIndex, endIndex)){
                        printw("Error attempting to remove tuple");
                        if (writer_ != nullptr)
                            writer_ << "Error attempting to remove tuple";
                        try{
                            printw("%s");
                            // close catalog and search file

                        }
                        catch (std::exception &e){
                            e.what();
                        }
                        // close data file
                        dataFile->closeFile();
                        return false;
                    }


                    try{
                        int val = boost::lexical_cast<int>(temp);

                        searchTree = intSearchTree;

                        boost::bind(BinarySearchTreeVisitor(), searchTree, val, keyIndex);

                        // delete key index from the index file
                        if (!boost::apply_visitor(BinarySearchTreeVisitor(), searchTree)){
                            printw("No index present in index file");
                            try{
                                printw(" ");

                            }
                            catch (std::exception &e){

                            }
                            // close data file
                            dataFile->closeFile();
                            return false;           
                        }
                    }
                    catch(boost::bad_lexical_cast &e){

                        /*
                         * Must be a std::string --- wow who knew
                         */

                        searchTree = stringSearchTree;

                        boost::bind(BinarySearchTreeVisitor(), searchTree, temp, keyIndex);

                        // delete key index from the index file
                        if (!boost::apply_visitor(BinarySearchTreeVisitor(), searchTree)){
                            printw("No index present in index file");
                            try{
                                printw(" ");
                                // close catalog and search file

                            }
                            catch (std::exception &e){
                                e.what();
                            }
                            // close data file
                            dataFile->closeFile();
                            return false;           
                        }

                    }                       

                    // clean up the index file
                    boost::bind(BinarySearchTreeVisitor(), searchTree, keyIndex, sizeToDelete);
                    boost::apply_visitor(BinarySearchTreeVisitor(), searchTree);

                    instances.erase(instances.begin());

                    for(int i= 0; i < instances.size(); i++){
                        instances.assign(i, instances.at(i) - 
                                                            sizeToDelete);
                    }

                }
}

关于致电boost::bind(),你应该使用boost::ref()当通过引用传递对象到按值接受相应参数的函数模板时,否则copy将尝试(在这种情况下会导致编译器错误,因为复制构造函数不可访问):

boost::bind(TreeVisitor(), boost::ref(tree), val, keyIndex);
//                         ^^^^^^^^^^^^^^^^

然而,这里有一个更大的问题:boost::variant只能保存可复制构造的类型。来自Boost.Variant 在线文档:

有界类型的要求如下:

  • CopyConstructible[20.1.3]。

  • 析构函数维护不抛出异常的安全保证。

  • 在变体模板实例化时完成。 (看boost::recursive_wrapper<T>对于接受不完整类型以启用递归变体类型的类型包装器。)

指定为模板参数的每个类型variant必须至少满足上述要求. [...]

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

提升::变体; std::unique_ptr 和复制 的相关文章

随机推荐

  • 检查属性是否有属性

    给定类中具有属性的属性 确定它是否包含给定属性的最快方法是什么 例如 IsNotNullable IsPK IsIdentity SequenceNameAttribute Id public Int32 Id get return Id
  • 我想我可以通过 Javascript 检测浏览器本身内部的 Tor 浏览器吗?

    如果浏览器是 Tor 浏览器 我想禁用我正在构建的网络应用程序的某些功能 我可以在浏览器本身 客户端 而不是服务器端 内部查明浏览器是否是 Tor 浏览器 我更喜欢一个不发出任何 HTTP 请求来将浏览器的 IP 与 Tor 出口节点进行匹
  • 使用 javascript 和 PhoneGap 的 HTML5 移动应用本地化

    我正在创建一个在所有 3 个移动平台 Android iOS 和 Windows Mobile 8 上运行的 HTML5 移动应用程序 我正在使用 javascript 进行本地化 https github com eligrey l10n
  • 如何在 Swift 中显示来自另一个类的警报?

    我有一个主课 AddFriendsController 运行以下代码行 ErrorReporting showMessage Error msg Could not add student to storage 然后我有这个ErrorRep
  • 从 python 中的列表中获取唯一值[重复]

    这个问题在这里已经有答案了 我想从以下列表中获取唯一值 nowplaying PBS PBS nowplaying job debate thenandnow 我需要的输出是 nowplaying PBS job debate thenan
  • 在python中通过调制解调器发送wav声音

    我正在尝试用 python 和 linux 制作一个自动应答和呼叫机 但到目前为止我只能拨打一个号码 当谈到发送声音或录制声音时 我没有成功 过去一周我一直在努力解决这个问题 到目前为止还找不到解决的方法 我使用的调制解调器是 Conexa
  • 可以嵌套
    吗?

    在 asp net 网页的内容页中 我想包含 paypal 按钮 立即付款 所以 我有一个母版页和一个内容页 在我的内容页面中 我复制粘贴贝宝代码 特别是 我使用 modalpopupextender 来允许我的用户购买该对象 问题是 它不
  • 了解 JAX-WS 中的 @Oneway 注释

    根据 javadoc 指示给定的 WebMethod 只有输入消息而没有输出 通常 单向方法在执行实际业务方法之前将控制线程返回给调用应用程序 如果标记为 Oneway 的操作具有返回值或 Holder 参数 或者声明任何已检查异常 181
  • UPDATE SET 中的子查询 (sql server 2005)

    我有一个关于在 Update 语句中使用子查询的问题 我的例子 UPDATE TRIPS SET locations city FROM select Distinct city from poi where poi trip guid t
  • Qt中如何从主窗口打开一个新窗口?

    我是 qt 编程新手 想知道如何在主窗口消失的情况下从主窗口打开一个新窗口 有没有源代码我可以看一下 从主窗口中的插槽调用以下代码 QWidget wdg new QWidget wdg gt show hide this will dis
  • 将字典保存到 UserDefaults

    我试图在 UserDefaults 中存储字典 并且在代码运行时总是导致应用程序崩溃 以下是执行时导致应用程序崩溃的示例代码 我尝试将其转换为 NSDictionary 或最初将其设为 NSDictionary 得到了相同的结果 class
  • String.getBytes("UTF-16") 在所有平台上都会返回相同的结果吗?

    我需要从包含用户密码的字符串创建哈希 为了创建哈希 我使用通过调用获得的字节数组String getBytes 但是 当我在不是默认编码的平台上使用指定编码 例如 UTF 8 调用此方法时 非 ASCII 字符会被默认字符替换 如果我正确理
  • Java 中的“类型不明确”错误是什么?

    在下面的代码中 我在最后一行收到编译器的错误 列表类型不明确 在尝试定义 cgxHist 列表的行上 我究竟做错了什么 import java awt import javax swing import java util public c
  • 学习Ruby:推荐阅读的博客? [关闭]

    Closed 这个问题不符合堆栈溢出指南 目前不接受答案 我即将开始学习 Ruby 并且想要一些阅读材料来帮助我学习 我正在寻找你的top 5Ruby 上的博客 新闻和任何 活跃的 公告板 我可以很好地处理新闻组 但我更喜欢在 BB 中阅读
  • MySQL“二进制”与“char字符集二进制”

    有什么区别binary 10 vs char 10 character set binary And varbinary 10 vs varchar 10 character set binary 它们是同义词吗allMySQL 引擎 有什
  • 将负 y 轴转换为正 (matplotlib)

    I want to plot bar chart for some parameters for men and women I have done like this 我想显示上侧 正 y 轴 的平均值和下侧 负 x 轴 女性的频率 在这
  • 使用 php 显示当前页面的活动导航

    我正在尝试使用 current url basename SERVER PHP SELF 为了确定我在哪个页面 考虑到我的导航 html 存储在 php 文件中并包含在每个页面中 这是我用来确定哪个导航选项应处于活动状态的代码 ul ul
  • NHibernate QueryOver 选择实体和聚合

    我想要做的是显示一个简单的数据网格 其中包含实体数据及其子项的聚合数据 例如 让我们使用订单和行项目 我想显示订单信息以及行项目数 订单 ID 订单日期 行项目数 现在通常在 SQL 中你可以通过多种方式做到这一点 但这是我能想到的在转换为
  • 将 TypeScript 编译器加载到 ClearScript 中,“WScript 未定义”,不可能完成的任务?

    我尝试使用清晰脚本加载打字稿编译器来编译一些基本的 TypeScript 代码 不幸的是 当执行 TypeScript 编译器源代码时 我收到此错误 WScript 未定义 这是LINQPad我使用过的程序 放置 ClearScript d
  • 提升::变体; std::unique_ptr 和复制

    这个问题确定了不可复制类型不能与Boost变体一起使用 Tree class template