typeof_unqual__typeof_unqual__ (C23)

C23 標準中的新功能是一元運算元,typeof_unqual會在捨棄 、 和 restrictconstvolatile限定符之後傳回表達式的類型。 它可以用於類型宣告、類型轉換、類型檢查等等。 它會取得變數、函式或任何 C 運算式的類型。

關鍵詞 __typeof_unqual__ 是 Microsoft 特定的擴充功能,提供與 typeof_unqual相同的功能。 關鍵詞 __typeof_unqual__ 與 只有在編譯所有 C 版本時才有不同 typeof_unqual 之處, /std:clatest而且它可能會簡化支援 的其他編譯程式 __typeof_unqual__之間的程式代碼移植。

typeof_unqual 語法

typeof_unqual(type)
typeof_unqual(constant-expression)
__typeof__unqual(constant-expression)

typeof_unqual 例子

這個範例會使用 typeof_unqual(),但如果您使用 __typeof_unqual__,則行為會相同。

// Compile with /std:clatest and /experimental:c11atomics
#include <stdatomic.h>

// A function that takes an atomic int pointer, but uses a non-atomic copy of the value
void func(_Atomic(int) * pAtomic)
{
    typeof_unqual(*pAtomic) local = *pAtomic;

    // Use local non-atomic copy of value
}

int main()
{
    int* const cpVar1 = 2;
    typeof_unqual(cpVar1) pVar2 = 3;
    pVar2 = 4; // no error because pi is not const. cpVar1 = 4 would be an error.

    _Atomic(int)i = 42;
    func(&i);

    return 0;
}

需求

需要 Visual Studio 17.9 或更新版本,或 cl.exe 19.39.33428 版或更新版本。 若要使用 typeof_unqual,請使用 編譯 /std:clatest

另請參閱

/std (指定語言標準版)