在MSVC复合文字 [英] Compound literals in MSVC
问题描述
在GCC,我能够做到这一点:
In GCC, I'm able to do this:
(CachedPath){ino}
inode->data = (struct Data)DATA_INIT;
其中:
struct CachedPath
{
Ino ino;
};
typedef int8_t Depth;
struct Data
{
Offset size;
Blkno root;
Depth depth;
};
#define DATA_INIT {0, -1, 0}
MSVC给出了这类铸件以下错误:
MSVC gives the following error for these kind of casts:
error C2143: syntax error : missing ';' before '{'
如何在MSVC做到这一点?还要注意,code已经从C99,在那里我用指定的初始化这个转换,然后将它转换类似。这些不同功能之间的C99如何相关,和C ++的MSVC / GCC实现的清晰度是AP preciated。
How can I do this in MSVC? Further note that the code has been converted from C99, where I used designated initializers for this, and then cast it similarly. Any clarity on how these various features relate between C99, and MSVC/GCC implementations of C++ is appreciated.
推荐答案
将构建(类型){initialisers}
是不是一个转换操作,但它是语法结构一个的复合文字的的。
这是一个C99的结构,它GCC也支持在C ++编译器作为一个扩展。至于我能确定,复合文字不会被MSVC在其任一C或C ++模式支持。
The construct (Type){initialisers}
is not a cast operation, but it is the syntactic construct of a compound literal.
This is a C99 construct, which GCC also supports in its C++ compiler as an extension. As far as I can determine, compound literals are not supported by MSVC in either its C or C++ mode.
有关此结构的替代品
- 明确声明并初始化所需的结构类型的临时对象和使用,而不是在分配字面该化合物
- 而不是做一个单一的分配与复合文字的,使用一个单独的分配每一个人会员。
这篇关于在MSVC复合文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!