小编vmr*_*rob的帖子

如果变量名存储为字符串,如何获取变量值?

如果我将变量名称作为字符串,如何检索bash变量值?

var1="this is the real value"
a="var1"
Do something to get value of var1 just using variable a.
Run Code Online (Sandbox Code Playgroud)

语境:

我有一些AMI(亚马逊机器映像),我想启动每个AMI的几个实例.一旦完成启动,我想根据其AMI类型设置每个实例.我不想在任何AMI中烘焙大量脚本或密钥,因此我准备了一个通用的启动脚本,并将其放在S3上,并提供可公开访问的链接.在rc.local中,我放了一小段代码来获取启动脚本并执行它.这就是我在AMI中所拥有的一切.然后,每个AMI访问一个通用配置脚本,该脚本适用于所有AMI和每个AMI的特殊设置脚本.这些脚本是私有的,需要签名URL才能访问它们.

所以现在,当我触发AMI的一个实例(my_private_ami_1)时,我会在S3上显示另一个文件的签名URL,该文件包含所有私有脚本在键/值对方面的签名URL.

config_url="http://s3.amazo.../config?signature"
my_private_ami_1="http://s3.amazo.../ami_1?signature"
...
Run Code Online (Sandbox Code Playgroud) 当启动脚本运行时,它会下载上面的文件并且source是它.然后它检查其AMI类型并为自己选择正确的设置脚本.

ami\_type=GET AMI TYPE #ex: sets ami\_type to my\_private\_ami\_1
setup\_url=GET THE SETUP FILE URL BASED ON AMI\_TYPE # this is where this problem arises
Run Code Online (Sandbox Code Playgroud)

所以现在我可以拥有一个通用代码,它可以触发实例而不管它们的AMI类型和实例可以自己处理.

string bash

122
推荐指数
6
解决办法
8万
查看次数

为什么Clang和VS2013接受移动大括号初始化的默认参数,但不接受GCC 4.8或4.9?

就像标题所暗示的那样,我有一个简短的演示程序可以编译所有这些编译器,但是在使用gcc 4.8和gcc 4.9进行编译后运行的核心转储:

任何想法为什么?

#include <unordered_map>

struct Foo : std::unordered_map<int,int> {
    using std::unordered_map<int, int>::unordered_map;
    // ~Foo() = default; // adding this allows it to work
};

struct Bar {
    Bar(Foo f = {}) : _f(std::move(f)) {}
    // using any of the following constructors fixes the problem:
    // Bar(Foo f = Foo()) : _f(std::move(f)) {}
    // Bar(Foo f = {}) : _f(f) {}

    Foo _f;
};

int main() {
    Bar b;

    // the following code works as expected
    // Foo f1 …
Run Code Online (Sandbox Code Playgroud)

c++ gcc c++11 list-initialization

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

何时应该在字符数组上使用std :: string?

当我设计类接口时,我坐在那里思考是否应该使用const char*const std::string&经常使用它,当它归结为它时,我常常觉得一只手中有6只,另一只手拿着半打.

采用以下两个函数原型:

void foo(const char* str);
void foo(std::string str);
Run Code Online (Sandbox Code Playgroud)

如果foo函数是存储字符串,我会说第二个是更好的选择,因为能够传递字符串并尽可能利用移动语义.但是,如果foo只需要读取字符串,const char*解决方案会更好吗?

从性能角度来看,std::string不需要创建临时的.但是,使用已经存在的字符串作为参数调用该函数看起来很突出:foo(mystr.c_str()).更糟糕的是,如果需要在未来的某个时刻对阵列进行更高级的操作,或者如果应该存储副本,则接口必须进行更改.

所以我的问题是:

是否有明确的定义,无论是个人的还是其他的,约定何时std::string或是const char*更好的选择?此外,在开始一个新项目时,最好是与使用保持一致,还是只考虑最适合当前代码块的哪一个?

c++ string character-arrays

33
推荐指数
3
解决办法
3411
查看次数

如何在删除元素时阻止重新散列std :: unordered_map?

我有一个std :: unordered_map,我将从via迭代中删除元素.

auto itr = myMap.begin();
while (itr != myMap.end()) {
    if (/* removal condition */) {
        itr = myMap.erase(itr);
    } else {
        ++itr;
    }
}
Run Code Online (Sandbox Code Playgroud)

我想阻止地图执行任何昂贵的操作,直到我完成删除我需要删除的所有元素.我有一个有效的顾虑吗?我误解了内部存储的工作原理吗?

c++ iterator unordered-map hashtable map

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

具有相同签名的模板函数重载,为什么这有效?

最小程序:

#include <stdio.h>

#include <type_traits>

template<typename S, typename T>
int foo(typename T::type s) {
    return 1;
}

template<typename S, typename T>
int foo(S s) {
    return 2;
}

int main(int argc, char* argv[]) {
    int x = 3;
    printf("%d\n", foo<int, std::enable_if<true, int>>(x));

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

    1 
Run Code Online (Sandbox Code Playgroud)

为什么不给出编译错误?生成模板代码时,功能不会int foo(typename T::type search)int foo(S& search)签名相同吗?

如果您稍微更改模板函数签名,它仍然有效(正如我所期望的上面的示例):

template<typename S, typename T>
void foo(typename T::type s) {
    printf("a\n");
}

template<typename S, typename T>
void foo(S s) {
    printf("b\n");
}
Run Code Online (Sandbox Code Playgroud)

然而,这没有,但唯一的区别是一个具有int签名而另一个由第一个模板参数定义. …

c++ templates overloading

14
推荐指数
2
解决办法
1400
查看次数

`git diff` 和 `git difftool` 有什么区别?

git diff和 和有git difftool什么区别?

我看到我可以编辑 git 的配置文件以轻松更改调用时使用的外部工具git difftool,并且似乎将git diff补丁输出到命令行。

我为什么要使用git diff

git

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

什么是PowerShell管道中的`.`简写?

我找了一个代码块我使用(从采购的另一个问题),我一直没能找出什么..{process表示这个片段(评论删除):

Get-ItemProperty $path |
.{process{ if ($_.DisplayName -and $_.UninstallString) { $_ } }} |
Select-Object DisplayName, Publisher, InstallDate, DisplayVersion, HelpLink, UninstallString |
Sort-Object DisplayName
Run Code Online (Sandbox Code Playgroud)

我知道,%For-EachObject?是简写形式,WhereWhere-Object,但接下来的问题是:

什么是.速记?

powershell

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

`template <class>朋友类Foo`是什么意思?

我正在探索boost :: iterator_facade并遇到了这段代码:

    friend class boost::iterator_core_access;
    template <class> friend class Iterator;
Run Code Online (Sandbox Code Playgroud)

第二行是什么意思?我熟悉朋友课程,但我认为我以前没见过template <class>任何东西.


这是上下文:

template <class Value>
class node_iter
  : public boost::iterator_facade<
        node_iter<Value>
      , Value
      , boost::forward_traversal_tag
    >
{
 public:
    node_iter()
      : m_node(0) {}

    explicit node_iter(Value* p)
      : m_node(p) {}

    template <class OtherValue>
    node_iter(node_iter<OtherValue> const& other)
      : m_node(other.m_node) {}

 private:
    friend class boost::iterator_core_access;
    template <class> friend class node_iter;

    template <class OtherValue>
    bool equal(node_iter<OtherValue> const& other) const
    {
        return this->m_node == other.m_node;
    }

    void increment()
    { m_node = m_node->next(); …
Run Code Online (Sandbox Code Playgroud)

c++ boost friend

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

std :: chrono :: time_point设置为now

我还是很新的库和我在std :: chrono上找到的文档对我不起作用.

我正在尝试实现一个包含时间戳的对象容器.对象将按照从最近到最近的顺序存储,我决定尝试使用std :: chrono :: time_point来表示每个时间戳.处理数据的线程将定期唤醒,处理数据,查看何时需要再次唤醒然后在该持续时间内休眠.

static std::chrono::time_point<std::chrono::steady_clock, std::chrono::milliseconds> _nextWakeupTime;
Run Code Online (Sandbox Code Playgroud)

我的印象是上面的声明使用了具有毫秒精度的替代时钟.

下一步是将_nextWakeupTime设置为now的表示;

_nextWakeupTime = time_point_cast<milliseconds>(steady_clock::now());
Run Code Online (Sandbox Code Playgroud)

该行不会编译:

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::chrono::time_point<_Clock,_Duration>' (or there is no acceptable conversion)
        with
        [
            _Clock=std::chrono::system_clock,
            _Duration=std::chrono::milliseconds
        ]
        chrono(298): could be 'std::chrono::time_point<_Clock,_Duration> &std::chrono::time_point<_Clock,_Duration>::operator =(const std::chrono::time_point<_Clock,_Duration> &)'
        with
        [
            _Clock=std::chrono::steady_clock,
            _Duration=std::chrono::milliseconds
        ]
        while trying to match the argument list '(std::chrono::time_point<_Clock,_Duration>, std::chrono::time_point<_Clock,_Duration>)'
        with
        [
            _Clock=std::chrono::steady_clock,
            _Duration=std::chrono::milliseconds
        ]
        and
        [
            _Clock=std::chrono::system_clock,
            _Duration=std::chrono::milliseconds
        ]
Run Code Online (Sandbox Code Playgroud)

我知道在Windows系统上,stead_clock与system_clock是一样的,但我不知道这里发生了什么.我知道我可以这样做:

_nextWakeupTime += …
Run Code Online (Sandbox Code Playgroud)

c++ time c++11 c++-chrono

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

有没有办法在 Visual Studio 中列出库依赖项?

我正在尝试使用 MSBuild 在 Windows Server 2008 上使用 Boost 和 Cinder 构建 C++ 项目,但没有成功。我已经在我的 Windows 7 机器上使用 VS2013 和 MSBuild 构建了相同的项目,但是在服务器上它说它无法打开名为“threadsafestatics.lib”的库

我从未听说过 threadsafestatics.lib,而且我的 Google-foo 也没有给我带来任何运气,无法在网上找到任何有关它的信息。

有没有办法找出我的项目的哪一部分依赖于这个库?

c++ msbuild linker boost visual-studio-2013

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