如果我将变量名称作为字符串,如何检索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类型和实例可以自己处理.
就像标题所暗示的那样,我有一个简短的演示程序可以编译所有这些编译器,但是在使用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) 当我设计类接口时,我坐在那里思考是否应该使用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*
更好的选择?此外,在开始一个新项目时,最好是与使用保持一致,还是只考虑最适合当前代码块的哪一个?
我有一个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)
我想阻止地图执行任何昂贵的操作,直到我完成删除我需要删除的所有元素.我有一个有效的顾虑吗?我误解了内部存储的工作原理吗?
最小程序:
#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签名而另一个由第一个模板参数定义. …
git diff
和 和有git difftool
什么区别?
我看到我可以编辑 git 的配置文件以轻松更改调用时使用的外部工具git difftool
,并且似乎将git diff
补丁输出到命令行。
我为什么要使用git diff
?
我找了一个代码块我使用(从采购的另一个问题),我一直没能找出什么.
在.{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
和?
是简写形式,Where
或Where-Object
,但接下来的问题是:
什么是.
速记?
我正在探索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) 我还是很新的库和我在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) 我正在尝试使用 MSBuild 在 Windows Server 2008 上使用 Boost 和 Cinder 构建 C++ 项目,但没有成功。我已经在我的 Windows 7 机器上使用 VS2013 和 MSBuild 构建了相同的项目,但是在服务器上它说它无法打开名为“threadsafestatics.lib”的库
我从未听说过 threadsafestatics.lib,而且我的 Google-foo 也没有给我带来任何运气,无法在网上找到任何有关它的信息。
有没有办法找出我的项目的哪一部分依赖于这个库?