問題描述
在 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 中執行此操作? 進一步注意,代碼已從 C99 轉換而來,我為此使用了指定的初始值設定項,然后進行了類似的轉換.任何有關 C99 和 C++ 的 MSVC/GCC 實現之間的各種功能之間關系的清晰性表示贊賞.
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.
推薦答案
構造 (Type){initialisers}
不是強制轉換操作,而是復合的句法構造字面意思.這是一個 C99 構造,GCC 在其 C++ 編譯器中也支持它作為擴展.據我所知,在 C 或 C++ 模式下,直到并包括 MSVC 2012 都不支持復合文字.對 C 模式的支持是后來在 MSVC 2013 中引入的.在 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 up to and including MSVC 2012, in either its C or C++ mode. The support in C mode was introduced later, in MSVC 2013. In C++ mode it is still not supported and I believe it is unlikely support will be added.
對于 MSVC 2012 及更早版本,此構造的替代方案是
For MSVC 2012 and older, the alternatives for this construct are
- 顯式聲明和初始化所需結構類型的臨時對象,并在賦值中使用它而不是復合字面量
- 不要對復合文字進行單一賦值,而是為每個單獨的成員使用單獨的賦值.
這篇關于MSVC 中的復合文字的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!