小编phi*_*inz的帖子

允许从 std::map 的键中窃取资源吗?

在 C++ 中,是否可以从我以后不再需要的地图中窃取资源?更准确地说,假设我有一个std::mapwithstd::string键,我想通过map使用std::move. 请注意,对键的这种写访问会破坏 的内部数据结构(键的顺序),map但之后我不会使用它。

问题:我可以在没有任何问题的情况下执行此操作,还是会导致意外错误,例如在析构函数中出现意外错误,map因为我std::map以非预期的方式访问它?

这是一个示例程序:

#include<map>
#include<string>
#include<vector>
#include<iostream>
using namespace std;
int main(int argc, char *argv[])
{
    std::vector<std::pair<std::string,double>> v;
    { // new scope to make clear that m is not needed 
      // after the resources were stolen
        std::map<std::string,double> m;
        m["aLongString"]=1.0;
        m["anotherLongString"]=2.0;
        //
        // now steal resources
        for (auto &p : m) {
            // according to my IDE, p has type 
            // std::pair<const …
Run Code Online (Sandbox Code Playgroud)

c++ move-semantics

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

函数指针作为模板参数

在以下C++代码中,该行bar<func_ptr>(); //does not work会导致编译错误:

#include <iostream>

using namespace std;

void foo(){
    cout<<"Hello world";
};

template<void(*func)()> 
void bar(){
    (*func)();
}

int main() {
    using fun_ptr_type= void(*)();
    constexpr fun_ptr_type func_ptr=&foo;

    bar<&foo>();     //works
    bar<func_ptr>(); //does not work
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

g ++的输出是这样的:

src/main.cpp: In function ‘int main()’:
src/main.cpp:19:16: error: no matching function for call to ‘bar()’
  bar<func_ptr>(); //does not work
                ^
src/main.cpp:10:6: note: candidate: template<void (* func)()> void bar()
 void bar(){
      ^~~
src/main.cpp:10:6: note:   template argument deduction/substitution failed:
src/main.cpp:19:16: error: ‘(fun_ptr_type)func_ptr’ …
Run Code Online (Sandbox Code Playgroud)

c++ templates function-pointers

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

使用局部变量的引用来初始化 constexpr 变量是否有效?

以下代码只能在 GCC 上编译(在 godbolt.org 上检查了 10.4 和 13.2),但不能在 Clang 上编译(在我尝试过的所有版本上都失败,例如 godbolt.org 上的 17.0.1):

struct A {
  static constexpr int b{1};
};

int main(int argc, char *argv[]) { 
    A a;
    A& aref{a};
    constexpr auto bb1{a.b};
    constexpr auto bb2{aref.b};
    return bb1+bb2; 
}
Run Code Online (Sandbox Code Playgroud)

叮当输出:

struct A {
  static constexpr int b{1};
};

int main(int argc, char *argv[]) { 
    A a;
    A& aref{a};
    constexpr auto bb1{a.b};
    constexpr auto bb2{aref.b};
    return bb1+bb2; 
}
Run Code Online (Sandbox Code Playgroud)

https://godbolt.org/z/nG4j3KefE

为什么?

c++ clang language-lawyer constexpr

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

为什么我可以有一个 constpair&lt;const T, T&gt; 对pair&lt;T, T&gt; 的引用?

我偶然发现我可以const std::pair<const int, int>&参考std::pair<int,int>

#include <utility>
int main()
{
    std::pair<int, int> p{1,2};
    const std::pair<const int, int>& ref_ok{p}; // why does this work?
    std::pair<const int, int>& ref_invalid{p}; // then why does this not work?
}
Run Code Online (Sandbox Code Playgroud)

考虑到const std::pair<const int, int>std::pair<int, int>是不通过继承相关的不同类型,为什么这是可能的?

c++ reference const-correctness

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

从固定大小的 std::span 创建固定大小的 std::array 的惯用方法是什么?

std::array<uint8_t,N>我正在尝试从 a创建 a,std::span<uint8_t,N>但我找不到一种没有memcpy、或 的方法来执行此操作std::copy,或者std::ranges::copy这不能保护我免受目标数组大小的错误指定。

#include <algorithm>
#include <array>
#include <iostream>
#include <span>

int main(int argc, char **argv) {
  constexpr size_t N = 10;
  std::array<uint8_t, N> original;
  std::span span(original); // of type std::span<uint8,N>

  std::array copy1(span);                               // does not work
  std::array<uint8_t, N> copy2(span);                   // does not work
  std::array<uint8_t, N> copy3(begin(span), end(span)); // does not work


  // ugly stuff that works, but does not protect me if I specify wrong array size
  constexpr size_t …
Run Code Online (Sandbox Code Playgroud)

c++ stdarray c++20 std-span

5
推荐指数
2
解决办法
305
查看次数

如何委托 std::variant 的构造函数?

我有两个结构A1A2另一个结构B有两个结构的构造函数。如何编写 的委托构造函数std::variant<A1,A2>

#include <variant>
struct A1 {};
struct A2 {};
using A = std::variant<A1, A2>;

struct B {
  B(const A1 &){}
  B(const A2 &){}
  B(const A& a)
      :B(a) // <--- what do I put here to delegate like std::visit ?
  {}
};

int main(int argc, char *argv[]) {
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种本着 的精神的干净的现代方式std::visitB我知道我可以使用静态成员函数通过Ausing和 lambda构造实例std::visit,但这不是我所要求的,因为我想依赖从A到 的隐式类型转换B

c++ constructor std-variant

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

朱莉娅:如何计算含有NaNs的载体的卷积?

Julia中的卷积函数具有以下行为:

               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.6.1 (2017-10-24 22:15 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-pc-linux-gnu

julia> conv([1,2,NaN],[1])

3-element Array{Float64,1}:
 NaN
 NaN
 NaN
Run Code Online (Sandbox Code Playgroud)

这是一个错误吗?或者这与使用的FFT算法有关conv

如果其中一个向量包含NaNs,我如何在Julia中计算卷积(不是出于性能原因而手动)?在此示例中,结果应为:

3-element Array{Float64,1}:
   1.0
   2.0 …
Run Code Online (Sandbox Code Playgroud)

convolution julia

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

这个C++代码异常是否安全?

我有来自mysql.com的以下代码:

/* Copyright 2008, 2010, Oracle and/or its affiliates. All rights reserved.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.

There are special exceptions to the terms and conditions of the GPL
as it is applied to this software. View the full text of the
exception in file EXCEPTIONS-CONNECTOR-C++ in the directory of …
Run Code Online (Sandbox Code Playgroud)

c++ exception

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

Julia 数组索引集的缩写

考虑两个不同大小的矩阵

\n
julia> A= ones(Float64,3,3)\n3\xc3\x973 Matrix{Float64}:\n 1.0  1.0  1.0\n 1.0  1.0  1.0\n 1.0  1.0  1.0\n\njulia> B = ones(Float64,2,2)\n2\xc3\x972 Matrix{Float64}:\n 1.0  1.0\n 1.0  1.0\n
Run Code Online (Sandbox Code Playgroud)\n

有没有简写

\n
julia> A[1:size(B)[1],1:size(B)[2]]\n2\xc3\x972 Matrix{Float64}:\n 1.0  1.0\n 1.0  1.0\n
Run Code Online (Sandbox Code Playgroud)\n

以像“”这样的好函数的形式julia> A[indices(B)](不幸的是它不存在ERROR: UndefVarError: indices not defined)?

\n

multidimensional-array julia

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

来自临时的 std::pair:为什么调用复制构造函数而不是移动构造函数?

考虑以下代码:

#include<iostream>
using namespace std;
struct A {
    A(const A&){
        cout<<"copy constructor"<<endl;
    }
    A(A&&){
        cout<<"move constructor"<<endl;
    }
    ~A(){
        cout<<"destructor"<<endl;
    }
    static std::pair<A,int> f1()
    {
        int i = 1;
        return std::pair<A,int>{i,2};
    }
    static std::pair<A,int> f2()
    {
        int i = 1;
        return std::pair<A,int>{A(i),2};
    }
    private:
    A(int){
        cout<<"constructor"<<endl;
    }
};

int main()
{
    cout<<"f1: "<<endl;
    A::f1();
    cout<<"f2: "<<endl;
    A::f2();
}
Run Code Online (Sandbox Code Playgroud)

构造函数A(int)是私有的,因此A不能pair<A,int>从 就地构造int。因此,在 中构建了一个临时的f1。在f2我显式创建临时文件但行为不同时,输出为:

f1: 
constructor
copy constructor
destructor
destructor
f2: 
constructor …
Run Code Online (Sandbox Code Playgroud)

c++ move std-pair

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