分享方式:


編譯器警告 (層級 3) C4839

非標準使用類別 ' type ' 做為 variadic 函式的引數

傳遞至 variadic 函式的類別或結構,例如 printf 必須可簡單複製。 傳遞這類物件時,編譯器只會進行位元複製,而且不會呼叫建構函式或解構函式。

此警告從 Visual Studio 2017 開始提供。

範例

下列範例會產生 C4839:

// C4839.cpp
// compile by using: cl /EHsc /W3 C4839.cpp
#include <atomic>
#include <memory>
#include <stdio.h>

int main()
{
    std::atomic<int> i(0);
    printf("%i\n", i); // error C4839: non-standard use of class 'std::atomic<int>'
                        // as an argument to a variadic function
                        // note: the constructor and destructor will not be called;
                        // a bitwise copy of the class will be passed as the argument
                        // error C2280: 'std::atomic<int>::atomic(const std::atomic<int> &)':
                        // attempting to reference a deleted function
}

若要更正錯誤,您可以呼叫會傳回可完整複製類型的成員函式,

    std::atomic<int> i(0);
    printf("%i\n", i.load());

對於使用 CStringW 建置和管理的字串,提供的 operator LPCWSTR() 應該用來將 物件轉換成 CStringW 格式字串所預期的 C 指標。

    CStringW str1;
    CStringW str2;
    // ...
    str1.Format("%s", static_cast<LPCWSTR>(str2));