次の方法で共有


コンパイラの警告 (レベル 3) C4839

可変個引数関数の引数としての、クラス 'type' の標準でない使用法

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() を使用して、書式設定文字列によって予期されている C ポインターに CStringW オブジェクトをキャストする必要があります。

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