小编wye*_*r33的帖子

是否复制和更新F#中的不可变记录类型共享或复制内存?

F#中不可变记录的复制和更新过程是共享还是复制内存?含义,在以下代码中

type MyRecord = {
    X: int;
    Y: int;
    Z: int 
    }

let myRecord1 = { X = 1; Y = 2; Z = 3; }
let myRecord2 = { myRecord1 with Y = 100; Z = 2 }
Run Code Online (Sandbox Code Playgroud)

为变量做myRecord1myRecord2共享内存X?更一般地说,是否有一个很好的引用,它准确表示F#中哪些不可变/持久数据结构主动共享内存?

.net f# functional-programming

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

是否可以强制`std :: make_shared`使用类的new运算符?

是否可以强制std::make_shared使用类的新运算符?这涉及另一个SO问题.根据该问题,std::make_shared使用自定义分配器:

从标准(§20.7.2.2.6shared_ptr创建):

效果:分配适合T类型对象的内存,并通过放置新表达式:: new(pv)T(std :: forward(args)...)在该内存中构造一个对象.

因此,我认为我可以使用自定义展示位置新运算符,但这似乎是错误的

// std::cout
#include <iostream>

// std::make_shared
#include <memory>

// Track what we're making
struct Foo {
    Foo() {
        std::cout << "Foo constructor" << std::endl;
    }
    Foo(Foo const & foo) {
        std::cout << "Foo copy constructor" << std::endl;
    }
    Foo(Foo && foo) {
        std::cout << "Foo move constructor" << std::endl;
    }
    Foo & operator = (Foo const & foo) {
        std::cout << "Foo copy assignment" << std::endl;
        return …
Run Code Online (Sandbox Code Playgroud)

c++ c++11 c++14

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

我们可以在可能的情况下使用返回值优化,而不是复制,而不是复制,语义不是吗?

是否有可能在可能的情况下编写C++代码,我们依赖于返回值优化(RVO),但是否则可以依靠移动语义?例如,由于条件,以下代码无法使用RVO,因此它将结果复制回:

#include <iostream>

struct Foo {
    Foo() {
        std::cout << "constructor" << std::endl;
    }
    Foo(Foo && x) {
        std::cout << "move" << std::endl;
    }
    Foo(Foo const & x) {
        std::cout << "copy" << std::endl;
    }
    ~Foo() {
        std::cout << "destructor" << std::endl;
    }
};

Foo f(bool b) {
    Foo x;
    Foo y;
    return b ? x : y;  
}

int main() {
   Foo x(f(true));
   std::cout << "fin" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

这产生了

constructor
constructor
copy
destructor
destructor
fin
destructor
Run Code Online (Sandbox Code Playgroud)

这是有道理的.现在,我可以通过更改行强制在上面的代码中调用移动构造函数

    return …
Run Code Online (Sandbox Code Playgroud)

c++ c++11 c++14

7
推荐指数
2
解决办法
592
查看次数

如何从C++ 14中的广义lambda捕获返回包含std :: unique_ptr的std :: function?

我们如何在C++ 14中返回std::function包含std::unique_ptr广义lambda捕获的a?具体来说,在以下代码中

// For std::function
#include <functional>

// For std::iostream
#include <iostream>

// For std::unique_ptr
#include <memory>

#if 0
std::function <void()> make_foo() {
    auto x = std::make_unique <int> (3);
    return [x=std::move(x)]() {
        std::cout << *x << std::endl;
    };
}
#endif

int main() {
    auto x = std::make_unique <int> (3);
    auto foo = [x=std::move(x)]() {
        std::cout << *x << std::endl;
    };
    foo();
}
Run Code Online (Sandbox Code Playgroud)

在使用GCC 4.9.2和C++ 14打开时,一切正常.具体来说,它表明广义lambda捕获工作.但是,当我们更改代码时#if 1,我们得到编译错误:

g++ -g -std=c++14 test01.cpp -o test01
In file …
Run Code Online (Sandbox Code Playgroud)

c++ lambda c++14

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

如何根据`std :: set`中的元素切割`std :: vector`

有没有一种很好的方法来切割std::vector基于元素的std::set?换句话说,std::set在向量中保存我想要的索引中的元素.当然,我可以用代码完成这个:

#include <set>
#include <vector>
#include <iostream>
#include <iterator>

template <typename T>
std::vector <T> slice(
    std::vector <T> const & x,
    std::set <unsigned int> const & I
) {
    auto y = std::vector <T> ();
    for(auto const & i : I)
        y.push_back(x[i]);
    return y;
}

int main() {
    auto x = std::vector <double> { 1.2, 2.3, 3.4, 4.5, 5.6};
    auto I = std::set <unsigned int> { 0, 3, 4};
    auto y = slice(x,I);
    std::copy(y.begin(),y.end(),std::ostream_iterator <double>( …
Run Code Online (Sandbox Code Playgroud)

c++

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

当复制elison失败时,有没有办法阻止移动构造函数后跟移动赋值运算符?

我有一种情况,我想用一个参数调用一个函数,并将结果返回到同一个参数

foo = f(foo);
Run Code Online (Sandbox Code Playgroud)

另外,我假设参数x非常大,所以我不想调用它的复制构造函数,而是调用它的移动构造函数.最后,我不想通过引用传递参数,因为我想f用另一个函数组合函数g.因此,这样的事情就好

foo = g(f(foo));
Run Code Online (Sandbox Code Playgroud)

是可能的.现在,使用移动语义,这几乎是可能的,如以下程序所示

#include <iostream>

struct Foo {
    Foo() {
        std::cout << "constructor" << std::endl; 
    } 
    Foo(Foo && x) {
        std::cout << "move" << std::endl;
    }
    Foo(Foo const & x) {
        std::cout << "copy" << std::endl;
    }
    ~Foo() {
        std::cout << "destructor" << std::endl;
    }
    Foo & operator = (Foo && x) {
        std::cout << "move assignment" << std::endl; 
        return *this;
    }
    Foo & operator = (Foo & …
Run Code Online (Sandbox Code Playgroud)

c++ c++11 c++14

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

使用 Rust 时处理派生宏中的辅助属性的简化方法是什么?

使用 Rust 时处理派生宏中的辅助属性的简化方法是什么?为了说明我正在寻找的内容,我定义了一个名为 的派生宏Duplicate,它创建一个新结构,其中包含旧结构中已由辅助属性标记的所有元素。基本上就是这样转

#[derive(Duplicate)]
struct MyStruct {

    #[dupe_me]
    x : Vec <f64>,

    y : bool,     

    #[dupe_me]
    z : char,     
}
Run Code Online (Sandbox Code Playgroud)

进入

#[derive(Debug)]
struct MyStructDuplicated {
    x : Vec <f64>,
    z : char,     
}
Run Code Online (Sandbox Code Playgroud)

代码的结构是

./mymacro/src/lib.rs
./mymacro/Cargo.toml
./mybin/src/main.rs
./mybin/Cargo.toml
Run Code Online (Sandbox Code Playgroud)

mymacro/Cargo.toml作为

[package]
name = "mymacro"
version = "0.1.0"
authors = ["blank"]
edition = "2018"

[lib]
proc-macro = true

[dependencies]
syn = "1.0.11"
quote = "1.0.2"
proc-macro2 = "1.0.6"
Run Code Online (Sandbox Code Playgroud)

lib.rs作为

[package]
name = "mymacro"
version = …
Run Code Online (Sandbox Code Playgroud)

macros rust rust-macros

6
推荐指数
0
解决办法
1575
查看次数

在具有钻石继承和虚拟基类的类中调用函数的策略

如果我们有钻石继承并使用公共虚拟基类,我们可以阻止第一个构造函数被多次调用.现在,我想对构造函数之外的函数做同样的事情.例如,代码:

#include <iostream>

struct A {
    virtual void foo() {
        std::cout << "A" << std::endl;
    }
};
struct B : virtual public A {
    virtual void foo() {
        A::foo();
        std::cout << "B" << std::endl;
    }
};
struct C : virtual public A {
    virtual void foo() {
        A::foo();
        std::cout << "C" << std::endl;
    }
};
struct D : public B, public C{
    virtual void foo() {
        B::foo();
        C::foo();
        std::cout << "D" << std::endl;
    }
};

int main() {
    D d; …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance c++11

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

如何在MacOS上使用CMake设置多个RPATH目录

我们如何在MacOS上的CMake中设置目标上的多个RPATH目录?在Linux上,我们可以使用以冒号分隔的列表:

set_target_properties(mytarget
    PROPERTIES
    INSTALL_RPATH "\$ORIGIN/../lib:\$ORIGIN/../thirdparty/lib"
)
Run Code Online (Sandbox Code Playgroud)

在MacOS上,我们可以在技术上添加冒号分隔列表并otool -l显示它,但不搜索这些目录:

set_target_properties(mytarget
    PROPERTIES
    INSTALL_RPATH "@loader_path/../lib:@loader_path/../thirdparty/lib"
)
Run Code Online (Sandbox Code Playgroud)

通常情况下,如果我要在MacOS上有多个RPATH目录,我会发送多个链接器标志,这些标志将单独显示otool -l.就像是:

g++-mp-4.7 mytarget.cpp -o mytarget -Wl,-rpath,@loader_path/../lib,-rpath,@loader_path/../thirdparty/lib
Run Code Online (Sandbox Code Playgroud)

这使:

Load command 15
          cmd LC_RPATH
      cmdsize 32
         path @loader_path/../lib (offset 12)
Load command 16
          cmd LC_RPATH
      cmdsize 48
         path @loader_path/../thirdparty/lib (offset 12)
Run Code Online (Sandbox Code Playgroud)

如何使用CMake重新创建此行为?

rpath cmake

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

如何在克隆后没有链接的情况下使用捆绑创建git仓库备份

git bundle从备份克隆后,有没有使用捆绑文件没有链接的地方来创建备份的方法?我想将我的存储库备份到具有所有历史记录的单个文件中,然后稍后将其还原。

目前,我正在使用以下方法创建备份:

$ mkdir here                  
$ cd here                                                                       
$ git init                                                                      
Initialized empty Git repository in /tmp/here/.git/                             
$ touch stuff                                                                   
$ git add stuff                                                                 
$ git commit -m "This is a file"                                                
[master (root-commit) c867bcf] This is a file                                   
 1 file changed, 0 insertions(+), 0 deletions(-)                                
 create mode 100644 stuff                                                       
$ git bundle create myrepo.bundle --all                                         
Counting objects: 3, done.                                                      
Writing objects: 100% (3/3), 212 bytes | 212.00 KiB/s, done.                    
Total 3 (delta 0), reused 0 (delta 0) …
Run Code Online (Sandbox Code Playgroud)

git

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

标签 统计

c++ ×6

c++11 ×4

c++14 ×4

.net ×1

cmake ×1

f# ×1

functional-programming ×1

git ×1

inheritance ×1

lambda ×1

macros ×1

rpath ×1

rust ×1

rust-macros ×1