可能重复:
C++:什么时候有挥发性关键字帮你?
我从来没有用过它,但我想知道为什么人们会用它?它到底是做什么的?我搜索了论坛,发现它只是C#或Java主题.
将结构类型变量的实例声明为volatile是否足够(如果其字段是以可重入代码访问的),还是必须将结构的特定字段声明为volatile?
换句话说,两者之间的语义差异(如果有的话)是什么:
typdef struct {
uint8_t bar;
} foo_t;
volatile foo_t foo_inst;
Run Code Online (Sandbox Code Playgroud)
和
typedef struct{
volatile uint8_t bar;
} foo_t;
foo_t foo_inst;
Run Code Online (Sandbox Code Playgroud)
我认识到将指针类型的变量声明为volatile(例如,volatile uint8_t*foo)只是告诉编译器foo指向的地址可能会改变,而不会声明foo指向的值.我不清楚类比是否适用于结构类型的变量.
我想将一个对象分配给同一类型的volatile对象,但是由于编译器错误而无法执行此操作.如何改变程序来实现呢?除了让它工作,为什么我不能直接做到这一点?
我在这里使用Visual Studio 2010作为编译器.
class A
{
public:
};
int _tmain()
{
A a;
volatile A va;
va = a; // compiler error:C2678 here
return 0;
}
Run Code Online (Sandbox Code Playgroud) 根据这个答案,应该编译以下代码而不会出错:
#include <type_traits>
namespace
{
struct A { int i; };
volatile A a{};
static_assert(std::is_volatile< decltype(a) >{});
static_assert(std::is_volatile< decltype(a.i) >{});
}
Run Code Online (Sandbox Code Playgroud)
但是有一个很难的错误:
main.cpp:10:1: error: static_assert failed
static_assert(std::is_volatile< decltype(a.i) >{});
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
Run Code Online (Sandbox Code Playgroud)
这是一个铿锵的错误还是我错过了一些实质性的东西?
额外:
#include <type_traits>
namespace
{
struct A { int i; };
const A a{};
static_assert(std::is_const< decltype(a) >{});
static_assert(std::is_const< decltype(a.i) >{});
}
Run Code Online (Sandbox Code Playgroud)
后一个代码片段的行为完全相同.
额外:
static_assert(std::is_volatile< std::remove_pointer_t< decltype(&a.i) > >{});
Run Code Online (Sandbox Code Playgroud)
不会导致错误.
你能帮我理解为什么编译器会给我这些错误信息吗?我相信易变物体的成员也是易变的.我在这里提到.但它表明,如果我们有一个结构:
struct someStruct
{
int d;
};
Run Code Online (Sandbox Code Playgroud)
'p'的定义如下:
volatile someStruct* volatile* p;
Run Code Online (Sandbox Code Playgroud)
&(*p)->d具有以下类型'int*volatile*'而不是'volatile int*volatile*'.下面是我正在处理的实际代码.
行(标记为错误1和2)是编译器抛出错误消息的位置:
#include <vector>
#include <windows.h>
using namespace std;
struct ThreadInfo
{
bool bWaiting = false;
bool bWorking = false;
};
struct lThreadInfo
{
ThreadInfo d;
lThreadInfo *pNextList = nullptr;
} volatile *volatile lThreads(nullptr);
thread_local ThreadInfo* currentThr(nullptr);
void CreateThread_(void (*pFunc)(ThreadInfo*))
{
volatile lThreadInfo* volatile* p = &lThreads;
for(; *p; p = &(*p)->pNextList); //**//error 1!**
*p = new lThreadInfo;
CreateThread(
nullptr, // default security attributes …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个struct. 其中一个不会更改,因此被声明const,但另一个可能会异步更改,因此我想制作它volatile。
我正在尝试使用const结构的实例来初始化volatile一个。但是,如果我使用volatile关键字编译器会抛出这个错误:
passing 'volatile rect' as 'this' argument of 'rect& rect::operator=(rect&&)' discards qualifiers [-fpermissive]at line 15 col 8
Run Code Online (Sandbox Code Playgroud)
#include <Arduino.h>
struct rect {
int x0;
int y0;
int width;
int height;
};
const rect outer = {0, 0, 10, 5};
volatile rect inner;
void setup() {
inner = {outer.x0 + 1, outer.y0 + 1,
outer.width - 2, outer.height - 2};
}
void loop() { …Run Code Online (Sandbox Code Playgroud)