以下代码没问题:
constexpr double square_cstxpr(double x) { return x * x; }
int main() {
const int test = 5;
constexpr double result = square_cstxpr((double)test);
}
Run Code Online (Sandbox Code Playgroud)
但是,如果类型test从更改const int为const double,则g ++会出现以下错误:the value of 'test' is not usable in a constant expression.
请在此处查看g ++的代码和输出:http://coliru.stacked-crooked.com/a/2fe9b176c2b23798
有人可以解释一下这种行为吗?
我想在一些字符串中删除变音符号.tr///应该做的但是失败了(见下文).我以为我有一个编码/解码问题,但我发现我的s///工作正如我所料.有人可以解释一下原因吗?
以下是我得到的结果示例:
my $str1 = 'èîü';
my $str2 = $str1;
$str1 =~ tr/î/i/;
print "$str1\n"; # => i?iii?
$str2 =~ s/î/i/;
print "$str2\n"; # => èiü
Run Code Online (Sandbox Code Playgroud)
请注意,tr///还修改了字符串的第一个和第三个字符,而不仅仅是中间字符.
编辑:我使用Ubuntu 16.04和Mate桌面环境.
在C++ 11之前,我使用过rand(),<cstdlib>并且选择在main()函数中生成(或不生成)生成器非常简单(例如),然后在libraryA中的某个函数生成的库中使用随机数.代码看起来像这样:
LibraryB(生成随机数,老式方式):
#include <cstdlib> // rand, RAND_MAX
double GetRandDoubleBetween0And1() {
return ((double)rand()) / ((double)RAND_MAX);
}
Run Code Online (Sandbox Code Playgroud)
主程序:
#include <cstdlib> // srand
#include <ctime> // time, clock
int main() {
bool iWantToSeed = true; // or false,
// decide HERE, applies everywhere!
if(iWantToSeed){
srand((unsigned)time(0) + (unsigned int)clock());
}
// (...)
}
Run Code Online (Sandbox Code Playgroud)
LibraryA(使用LibraryB中的随机数,根据给出的种子生成main()):
#include "../folderOfLibraryB/Utils_random.h" // GetRandDoubleBetween0And1
void UseSomeRandomness(){
for(int i = 0; i < 1000000; i++){
double nb = GetRandDoubleBetween0And1();
// (...)
} …Run Code Online (Sandbox Code Playgroud) 根据
https://gcc.gnu.org/projects/cxx-status.html,g ++版本7与flag一起使用-std=c++1z,支持类模板的模板参数推导.
我希望编译以下代码,特别是作为Base一个抽象类,因此:
1.编译器知道没有Base可以创建的实例;
2.指向base的指针指向pt_base一个明确定义的实例(即Derived<int>{42}),其中type(int)是显式的.
template<typename ValueType>
class Base {
public:
virtual ValueType getValue() = 0;
};
template<typename ValueType>
class Derived : public Base<ValueType>{
public:
Derived(ValueType argt){ value = argt; }
virtual ValueType getValue(){ return value; }
ValueType value;
};
int main(){
Base *pt_base = new(Derived<int>{42}); // *ERROR*
delete pt_base;
}
Run Code Online (Sandbox Code Playgroud)
然而,它没有编译.G ++抱怨" 模板占位符类型'Base'必须后跟一个简单的declarator-id "; 如果我理解正确,它不会推断出模板参数.
遗憾是因为我想动态决定哪个派生类pt_base指向(可能是来自类Derived<someType>或类的对象Derived2<someType2>).这样,一个数组或一个vector<Base *> …
目前,我使用这样的东西:
my %tmpHash = routineReturningHash();
my $value = $tmpHash{'someKey'};
Run Code Online (Sandbox Code Playgroud)
我唯一需要的是$value,我不需要%tmpHash自己.所以我很想知道是否有办法避免声明%tmpHash.
我试过了
my $value = ${routineReturningHash()}{'someKey'};
Run Code Online (Sandbox Code Playgroud)
但它不起作用并输出一个奇怪的错误:" Can't use string ("1/256") as a HASH ref while "strict refs" in use".
有什么想法可以做到吗?
我想要一个Perl脚本来显示一个简单的gnuplot图.我不想将任何数据存储在文件中,我想使用gnuplot one-liner,例如:
gnuplot -p <(echo -e 'plot "-"\n1 1\n2 3\n3 1.7\n4.5 5\ne')
Run Code Online (Sandbox Code Playgroud)
这显示了点(1,1),(2,3),(3,1.7)和(4.5,5).
在Perl脚本中,我尝试过类似的东西
$plotString = "\"<(echo -e 'plot \\\"-\\\"\\n";
$plotString .= "1 1\\n2 3\\n3 1.7\\n4.5 5\\ne')\"";
system('gnuplot -p ' . $plotString);
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
-e plot "-"
^
"<(echo -e 'plot "-"\n1 1\n2 3\n3 1.7\n4.5 5\ne')", line 1: invalid command
Run Code Online (Sandbox Code Playgroud)
这个错误让我感到惊讶,因为传递给的字符串system(),如错误消息中引用的那样,显然是正确的.
不知道如何修改$plotString,以便system()将正确地解释gnuplot的命令?
次要问题:如何绘制图表with lines?(即使在Perl之外,我也无法使用gnuplot单行程来完成它.)
编辑:我的操作系统是Ubuntu 16.04.