小编Jup*_*ter的帖子

为什么基于范围的for-loop中的结构化绑定只是一个副本而不是一个引用?

我有以下代码:

#include "stdafx.h"
#include <unordered_map>
#include <cassert>
int main()
{
    struct Foo { int a; };
    std::unordered_map<int, Foo> foos{ { 0, { 3 } }, { 1, { 4 } } };
    for (auto &[i, foo] : foos)
    {
        foo.a = 6; //doesn't change foos[i].a
        assert(&foo.a == &foos[i].a); //does not pass
    }

    auto &[i, foo] = *foos.begin();
    foo.a = 7; //changes foo[0].a
    assert(&foo.a == &foos[0].a); //passes
}
Run Code Online (Sandbox Code Playgroud)

我的问题:

为什么第一个断言语句在第二个传递时没有通过?为什么我不能改变的价值foofoos地图中基于范围循环?

编译: MSVS ++ 17 Visual studio 15.3.2 …

c++ for-loop visual-c++ c++17 structured-bindings

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

初始化在类默认ctor类型的#define中定义的非静态成员数组

我有以下课程:

//in some .h file
#define BARS_IN_FOO 5 //The only place where this number should be specified.
//All code should work when I change this

//in some .cpp file
struct Foo;
struct Bar { Foo & foo; Bar(Foo & foo) : foo{ foo } {} } //Cannot be default initialized

struct Foo {
    std::array<Bar, BARS_IN_FOO> myBars;
    Foo() :
        myBars{ } //Error. Cannot default initialize Bars.
    //I want to initialize all Bars with Bar{*this} but at this point I don't
    //know …
Run Code Online (Sandbox Code Playgroud)

c++ arrays constructor

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

实例化一个 TypeVar 类型

作为 C++ 程序员,以下代码对我来说似乎很自然,但它无法运行:

from typing import TypeVar, Generic, List, NewType

TPopMember = TypeVar('TPopMember')
Population = NewType('Population', List[TPopMember])
class EvolutionaryAlgorithm(Generic[TPopMember]):
    def __init__(self, populationSize: int) -> None:
        # The following raises TypeError: 'TypeVar' object is not callable
        self.__population = Population([TPopMember() for _ in range(populationSize)])
Run Code Online (Sandbox Code Playgroud)

显然,Python 无法实例化实际上是 TypeVars 的类(TPopMember)。我只是想创建一个列表(人口),其中有几个默认初始化(在 Python 中你怎么说?)TPopMembers。我应该怎么做?

我正在使用 Python 3.7.2。

python type-hinting python-3.x

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

接口类型的默认值

这可能是一个愚蠢的问题,我忽略了一个明显的答案,但如果我有以下内容:

interface A { }
struct B {
    A some_field;
}
Run Code Online (Sandbox Code Playgroud)

然后,B的默认ctor会让我看到它的大小是多少?

换句话说(从另一个角度来看),接口类型的默认值是什么,它会被视为引用或值类型,因为类(引用)和结构(值)都可以从它继承?

c# struct interface

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

堆上的结构数组未正确初始化

我以为我知道如何处理c ++中的内存管理,但这让我很困惑:

请考虑以下代码:

struct A {
    int i;
};

int main(int argc, char* argv[]) {
    A a{ 5 }; //Constructs an A object on the stack
    A* b = new A{ 7 }; //Constructs an A object on the heap and stores a pointer to it in b
    A* c = new A[] { //Construct an array of A objects on the heap and stores a pointer to it in c
        { 3 },
        { 4 },
        { 5 }, …
Run Code Online (Sandbox Code Playgroud)

c++ arrays heap structure

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

使用 Node 在 VSCode 中调试 TS:未知文件扩展名“.ts”

我正在 VSCode 中设置一个在 Node 16 中运行的新 Typescript 项目。我正在尝试让调试器工作。

按 F5 时出现此错误:

Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
Run Code Online (Sandbox Code Playgroud)

当我在 package.json 中将 type 设置为 Module 时,我得到:

Uncaught TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for c:\Users\peterB\repositories\BlockTanks\blocktanks-bots\src\main.ts
Run Code Online (Sandbox Code Playgroud)

我的设置:

  • 我在 Node 16.13.2 中运行我的代码
  • 我用 Gulp 构建。构建成功,我可以node ./dist_dev/main.js成功运行我的代码。
  • 我想在“启动模式”下使用 VSCode 调试器,而不是附加到单独的进程。

我的 tsconfig.json:

{
    "references": [
        { "path": "src" }
    ],
    "compilerOptions": {
        /* Visit https://aka.ms/tsconfig.json to read more about this file */ …
Run Code Online (Sandbox Code Playgroud)

node.js typescript visual-studio-code

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

提供移动参数的构造函数的惯用方式

假设我有以下课程:

#include <vector>
class Foo
{
public:
    Foo(const std::vector<int> & a, const std::vector<int> & b)
        : a{ a }, b{ b } {}
private:
    std::vector<int> a, b;
};
Run Code Online (Sandbox Code Playgroud)

但是现在我想考虑构造函数的调用者可能会将临时变量传递给它的情况,并且我想将这些临时变量正确地移动到ab

现在我真的需要再添加 3 个构造函数,其中 1 个a作为右值引用,其中 1 个b作为右值引用,1 个只有右值引用参数吗?

当然,这个问题可以推广到任何数量的值得移动的参数,并且所需构造函数的数量将是参数^2 2^参数。

这个问题也适用于所有函数。

这样做的惯用方法是什么?还是我在这里完全遗漏了一些重要的东西?

rvalue-reference move-semantics c++11

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