小编Dmi*_*kov的帖子

如何避免在std :: pair中"隐式"调用单参数构造函数

最初的问题是如何以std::map<std::wstring, std::wstring> >安全的方式使用,因为相同类型的密钥和值极易出错.所以我决定为这个值创建一个简单的包装器:

    struct ComponentName
    {
      std::wstring name;

      // I want to prohibit any implicit string-ComponentName conversions!!!
      explicit ComponentName(const std::wstring& _name) : name(_name)
      {
      }

      bool operator<(const ComponentName& item_to_compare) const
      {
        return name < item_to_compare.name;
      }
    };

    typedef std::map<std::wstring, ComponentName> component_names_map;
Run Code Online (Sandbox Code Playgroud)

但是以下代码运行良好!

component_names_map component_names;
// Are you sure that ComponentName's constructor cannot be called implicitly? ;)
component_names_map::value_type a_pair = std::make_pair(L"Foo", L"Bar");
Run Code Online (Sandbox Code Playgroud)

它的工作原理是因为std::pair<std::wstring, ComponentName>复制构造函数显式使用ComponentName的字符串构造函数来分配std::pair<std::wstring, std::wstring>实例.这是绝对合法的操作.但它看起来像是ComponentName构造函数的"隐式"调用.

所以我知道问题的原因,但我怎样才能避免这种'隐含' wstring-ComponentName转换呢?最简单的方法是不声明字符串构造函数,但它使ComponentName初始化不方便.

c++ stl

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

Git:如何检查预推钩中当前推动的分支?

在我的项目中,我有一个预推git钩子,可以在每次推送时运行单元测试.

但是当我尝试在另一个分支上推送更改时,单元测试将针对活动分支运行,而不是针对当前推送的分支运行.

例如,如果我尝试从new_feature分支推送更改,而我的工作目录反映了develop分支的结构,则pre-push hook将运行develop分支的单元测试,而不是new_feature.

摆脱这个的基本想法是在预推钩中检查当前被推动的分支.但我不知道如何在钩子内获取有关当前推送分支的信息:这些信息不包含在钩子参数中.

git githooks

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

如何将3元素结构的stl向量转换为2D C风格的数组

假设我有以下简单的结构:

struct Vector3
{
    double x;
    double y;
    double z;
};
Run Code Online (Sandbox Code Playgroud)

我创建了一个顶点列表:

std::vector<Vector3> verticesList;
Run Code Online (Sandbox Code Playgroud)

除此之外,我还需要使用第三方库.该库具有以下签名的功能:

typedef double[3] Real3;
external void createMesh(const Real3* vertices, const size_t verticesCount);
Run Code Online (Sandbox Code Playgroud)

转换verticesList成可以createMesh()作为vertices参数传递的东西的最佳方法是什么?

目前我使用以下方法:

static const size_t MAX_VERTICES = 1024;

if (verticesList.size() > MAX_VERTICES)
    throw std::exception("Number of vertices is too big");

Real3 rawVertices[MAX_VERTICES];
for (size_t vertexInd = 0; vertexInd < verticesList.size(); ++vertexInd)
{
    const Vector3& vertex = verticesList[vertexInd];

    rawVertices[vertexInd][0] = vertex.x;
    rawVertices[vertexInd][1] = vertex.y;
    rawVertices[vertexInd][2] = vertex.z;
}

createMesh(rawVertices, verticesList.size()); …
Run Code Online (Sandbox Code Playgroud)

c++ arrays stl

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

标签 统计

c++ ×2

stl ×2

arrays ×1

git ×1

githooks ×1