小编CJC*_*ink的帖子

使用std :: function和std :: bind来存储回调并处理对象删除.

我想实现一个管理器,它使用C++ 11将回调存储到多态类的成员函数中.问题是我不知道如何处理成员所属的对象被删除或应该被删除的情况,我想让界面尽可能简单.

所以我想到了以下内容:将a存储std::weak_ptr到对象以及存储std::function到成员.

以下似乎有效:

class MyBase {
public:
    MyBase() {}
    virtual ~MyBase() {}
};
//--------------------------------------------------

class MyClass : public MyBase {
public:
    MyClass() : MyBase() {}
    void myDouble(double val) const { std::cout << "Value is: " << val << std::endl; }
};
//--------------------------------------------------

Class Manager {
public:
    void setFunction(std::weak_ptr<MyBase> base, std::function<void(double)> func) {
        m_function.first  = base;
        m_function.second = func;
    }
private:
    std::pair<std::weak_ptr<MyBase>, std::function<void(double)>> m_function;
};
Run Code Online (Sandbox Code Playgroud)

要使用它:

Manager db;
std::shared_ptr<MyClass> myClass = std::make_shared<MyClass>();
db.setFunction(myClass, std::bind(&MyClass::myDouble, myClass, std::placeholders::_1)); …
Run Code Online (Sandbox Code Playgroud)

c++ stdbind c++11 std-function

10
推荐指数
1
解决办法
1827
查看次数

别名的模板别名的类模板参数推导 (C++20)

我正在使用boost::outcome_v2::result错误的自定义类型和该类型的别名。

现在我正在努力让类模板参数推导适用于我的别名。

代码:

class my_error{ ... };

template <typename result_t>
using my_result = boost::outcome_v2::result<result_t, my_error>;
Run Code Online (Sandbox Code Playgroud)

问题是,当我从函数返回此类型时,我无法对结果类型使用类模板参数推导:

const my_result res = some_function();
Run Code Online (Sandbox Code Playgroud)

但如果我直接使用它就有效boost::outcome_v2::result

const boost::outcome_v2::result res = some_function();
Run Code Online (Sandbox Code Playgroud)

有没有办法为我的别名编写推导指南,以便它也能正常工作?

编译器资源管理器链接

c++ templates boost c++20

2
推荐指数
1
解决办法
104
查看次数

如何使用 MSVC 构建工具从命令行构建 SLN 文件

我正在尝试使用 MSVC 2017 构建工具在 GitHub 上构建 Hunspell 项目: https://github.com/hunspell/hunspell

该项目有一个msvc包含Hunspell.slnlibhunspell.vcxproj文件的文件夹(我对库感兴趣)

我的设置:

  • Windows 10
  • MSVC2017构建工具(15.9.3)
  • Windows 10 套件:10.0.17763.0

如果我打开“VS 2017 的 x86 Native Tools 命令提示符”并尝试构建解决方案,我会收到以下错误:

>msbuild libhunspell.vcxproj
Microsoft (R) Build Engine version 15.9.20+g88f5fadfbe for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 2018/12/01 6:01:18 PM.
Project "c:\Development\Hunspell\hunspell-1.7.0\msvc\libhunspell.vcxproj" on node 1 (default targets).
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Platforms\Win32\PlatformToolsets\v140_xp\Toolset.targets(36,5): warning MSB8003: Could not find WindowsSdkDir_71A variable from the registry.  TargetFrameworkVersion or PlatformToolset may be set to …
Run Code Online (Sandbox Code Playgroud)

msbuild build-tools visual-studio visual-studio-2017-build-tools

1
推荐指数
1
解决办法
1万
查看次数