我有以下程序:
struct A{ int i; };
int main()
{
const int i = 0;
auto ai = i;
ai = 2; // OK
const A buf[2];
for(auto& a : buf)
{
a.i = 1; // error!
}
std::cout << buf[0].i << buf[1].i << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
第一个auto ai = i;没有问题,似乎auto没有检索c/v限定符,因为ai可以修改但for循环失败编译为 - 错误:A::i在只读对象中分配成员
我知道这auto不会检索&功能,我的问题是:auto在我的情况下检索c/v限定符吗?我的测试程序似乎给出了相互矛盾的提示.
语言标准说:
[注意:第5节定义了语法,评估顺序和表达式的含义.58表达式是指定计算的运算符和操作数的序列.表达式可能会导致值,并可能导致副作用. - 结束说明]
我的代码如下:
int i=1;
A obj;
Run Code Online (Sandbox Code Playgroud)
那么,上面的两个陈述都算作"表达式"吗?
stackoverflow上的一些人说"int i = 1;" 不是表达.这对我来说很奇怪.
(1)初始化是一种"计算",对吧?所以它应该被视为"表达"?
(2)对象; //调用一个ctor.ctor是一种计算,所以它应该被视为"表达"?
sudo apt-get install libuv
$ sudo apt-get install libuv
[sudo] username ...
It fails to find package and install.
Run Code Online (Sandbox Code Playgroud)
libuv是否包含在其他一些软件包中?
我发现它们是不同的,语言标准规定了每个语句应该检索哪种类型(变量和表达式之间的差异)。但我真的很想知道为什么这两种类型应该不同?
#include<stdio.h>
int x=0;
decltype((x)) y=x;
int main()
{
y=2;
printf("%d,",x);
decltype((1+2))&z=x;//OK (1+2) is an express, but why decltype should differ?
z=3;
printf("%d\n",x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
运行结果是'2,3'
那么为什么decltype((int))是int&设计,这里C++语言设计的考虑是什么?任何需要这种设计的语法一致性?(我不想得到“这是设计使然”)
谢谢你的解释。
PS C:\Users\Hind> $b=@{}
PS C:\Users\Hind> $b+={k="a";v="b"}
A hash table can only be added to another hash table.
At line:1 char:1
+ $b+={k="a";v="b"}
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : AddHashTableToNonHashTable
Run Code Online (Sandbox Code Playgroud)
为什么失败了?如何成功地向哈希表添加一个元素?
我正在使用Mac,我在下面定义了这个别名.bashrc:
$cat .bashrc | grep la
alias la='ls -la'
Run Code Online (Sandbox Code Playgroud)
然后我尝试在脚本中使用它:
$cat ./mytest.sh
#!/bin/bash
la
Run Code Online (Sandbox Code Playgroud)
它运行并说它找不到la:
./mytest.sh: line 2: la: command not found
Run Code Online (Sandbox Code Playgroud)
为什么是这样?我试过Mac和Linux,同样的错误!
我希望unordered_set用我自己的哈希函数进行测试:
#include<unordered_set>
#include<iostream>
#include<functional>
using namespace std;
struct node{
size_t value;
bool operator == (const node& n){return value == n.value;}
};
size_t h(const node& n){
return n.value;
}
int main(){
unordered_set<node, std::function<size_t(const node&)>> s2(3,h);//failed
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我试图编译它,而clang给出了大量的错误:
clang++ m.cpp -std=c++11
In file included from m.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/unordered_set:324:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/functional:659:21: error: invalid operands to binary
expression ('const node' and 'const node')
{return __x == __y;}
~~~ ^ ~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__hash_table:2175:32: note: in instantiation of member
function 'std::__1::equal_to<node>::operator()' …Run Code Online (Sandbox Code Playgroud) 如下:
IntStream iStream = IntStream.range(1,4);
iStream.forEach(System.out::print);
List list1 = iStream.collect(Collectors.toList());//error!
Run Code Online (Sandbox Code Playgroud)
Java 1.8编译器提供类型推导错误.类似的代码可以用于String类型:
List<String> ls = new ArrayList<>();
ls.add("abc");
ls.add("xyz");
List list2 = ls.stream().collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
为什么?IntStream/LongStream/DoubleStream的工作方式与其他类型不同吗?如何修复我的编译错误?
我在jdk 8样本中看到过这种声明:
Map<String, ?> map = new HashMap<>(3);//OK
Run Code Online (Sandbox Code Playgroud)
但当我试图为"地图"增加价值时,我没有成功:
map.put("abc", Optional.of(5));
map.put("kk", "xyz");
Run Code Online (Sandbox Code Playgroud)
两者都无法编译.我想知道:
(1)"什么"?在上面的Map声明中注明?
(2)如何给这个"地图"赋值?
如何将此选项添加到目标描述部分?
谢谢。
c++ ×4
c++11 ×3
compilation ×2
expression ×2
java ×2
types ×2
alias ×1
apt-get ×1
auto ×1
bash ×1
bazel ×1
clang ×1
collect ×1
const ×1
declaration ×1
decltype ×1
dictionary ×1
element ×1
environment ×1
generics ×1
hash ×1
hashtable ×1
install ×1
java-stream ×1
libuv ×1
linux ×1
powershell ×1
target ×1
tolist ×1
ubuntu ×1
variables ×1