#include<iostream>
using namespace std;
struct B{};
struct A
{
A(const B &)
{
cout<<"A(const B &)"<<endl;
}
A(B &&)
{
cout<<"A(B &&)"<<endl;
}
};
A get()
{
B b;
return b;
}
int main()
{
get();
}
Run Code Online (Sandbox Code Playgroud)
我用VC++ 14.2和GCC 5.4.0测试了代码,它们都输出:
A(B &&)
Run Code Online (Sandbox Code Playgroud)
为什么输出不是
A(const B &)
Run Code Online (Sandbox Code Playgroud)
?
这段代码与此有何关系copy elision?(但A和B是不同的类型,所以copy elision不应该在这里工作)
我不是问decltype((x)),我知道它是如何工作的.
根据N4687草案,§10.1.7.2
4 For an expression e, the type denoted by decltype(e) is defined as follows:
...
(4.2) — otherwise, if e is an unparenthesized id-expression or an unparenthesized class
member access (8.2.5), decltype(e) is the type of the entity named by e. If
there is no such entity, or if e names a set of overloaded functions, the
program is ill-formed;
...
Run Code Online (Sandbox Code Playgroud)
例如
struct A { double x; };
const A* a = new A();
decltype(a->x) x3; …Run Code Online (Sandbox Code Playgroud) class A
{
struct B{};
public:
static void test(A::B){}
};
struct C
{
template<class T>
operator T()
{
return T();
}
};
int main()
{
A::test(C());
}
Run Code Online (Sandbox Code Playgroud)
此代码适用于clang 3.7,gcc 5.1和vc ++ 14.2.
2问题,
1.为什么模板推导出的类型是A :: B?(太聪明了!)
据我所知,模板通过return语句而不是参数来推断类型.
但是我发现了一些对N4606 12.3.2感兴趣的东西A conversion function template shall not have a deduced return type (7.1.7.4).(但是,我找不到关于此的更多信息,因为7.1.7.4太难理解了.)
2.为什么转换函数模板可以访问A :: B?
谢谢.
我同意何时使用引用与指针的答案.
但是,我想知道为什么C++将atomic_load定义为
template<class T>
T atomic_load(const std::atomic<T> *obj) noexcept;
^
Run Code Online (Sandbox Code Playgroud)
代替
template<class T>
T atomic_load(const std::atomic<T> &obj) noexcept;
^
Run Code Online (Sandbox Code Playgroud)
谁能帮我?
更新3:
在了解了"内存顺序"之后,我知道问题与编译器完全无关.
是的,因为我的CPU架构是Intel x86,无论我编写什么代码,内存顺序效果都不会发生.
更新2:
我检查反汇编代码.但是,我发现无论我如何添加代码,x.store总是在y.store之前.
问题应该来自编译器(它没有重新排序这些代码)而不是CPU(据我所知).
更新:
在我阅读评论后,似乎我必须借用一台CPU为alpha,arm或ppc的机器.
有谁知道我在哪里可以使用这种机器,即使这不是免费的?
来源:
我正在测试下面的代码.
atomic<int> x(0);
atomic<int> y(0);
void thr1()
{
x.store(1,memory_order_relaxed);
y.store(1,memory_order_relaxed);
}
void thr2()
{
while(!y.load(memory_order_relaxed))
;
cout<<x.load(memory_order_relaxed)<<endl; //may 0 or 1
}
Run Code Online (Sandbox Code Playgroud)
我知道输出可能是0.
但是,无论我尝试了多少次,我总是得到1.
这是因为我的CPU是x86架构吗?
如果没有,如何解决这个问题?
(顺便说一句,我知道CppMem.但它不能使用循环.)
那里.正如标题所说.
使用#include"test.h"是合法的.
使用#include"test/test.h"也是合法的.
但使用合法#include"../test.h"吗?
例如,#pragma once几乎每个编译器都是合法的.
但这不是C++的标准.
我找不到任何说"保证"是父目录含义的文件.
谁能帮我?