【发布时间】:2012-07-06 17:21:58
【问题描述】:
使用const,如注释所示,msvc 11 和 g++ 4.7.0 拒绝编译这个:
#include <memory> // std::unique_ptr
#include <utility> // std::move
using namespace std;
struct CommandLineArgs
{
typedef unique_ptr<
wchar_t const* const [],
void(*)( wchar_t const* const* )
> PointerArray;
//PointerArray const args; // Oops
PointerArray args;
int const count;
static wchar_t const* const* parsed(
wchar_t const commandLine[],
int& count
)
{
return 0;
}
static void deallocate( wchar_t const* const* const p )
{
}
CommandLineArgs(
wchar_t const commandLine[] = L"",
int _ = 0
)
: args( parsed( commandLine, _ ), &deallocate )
, count( _ )
{}
CommandLineArgs( CommandLineArgs&& other )
: args( move( other.args ) )
, count( move( other.count ) )
{}
};
int main()
{}
错误消息似乎不是特别有用,但这里是 g++ 的输出:
main.cpp:在构造函数“CommandLineArgs::CommandLineArgs(CommandLineArgs&&)”中: main.cpp:38:38:错误:使用已删除的函数 'std::unique_ptr::unique_ptr(const std::unique_ptr&) [w i _Tp = 常量 wchar_t* 常量; _Dp = void (*)(const wchar_t* const*); std::unique_ptr = std::unique_ptr]' 在 c:\program files (x86)\mingw\bin\../lib/gcc/mingw32/4.7.0/include/c++/memory:86:0 包含的文件中, 来自 main.cpp:1: c:\program files (x86)\mingw\bin\../lib/gcc/mingw32/4.7.0/include/c++/bits/unique_ptr.h:402:7: 错误:在此声明为什么?
【问题讨论】:
-
-1,OP 没有努力使其成为 SSCE,如果
CommandLineArgs仅包含 const 成员和移动 ctor,那么问题将很明显。 -
@Abyx:我在聊天中告诉过你,你不必提交更多证据。够了。请让它休息:它伤害了我(代表你)。好的,为您解释:当类仅包含 const 成员时会产生错误消息。如果有的话,这能告诉你什么?
-
这是一个 SSCE - ideone.com/VOlcA ,编译器错误清楚地说明了那里出了什么问题。
-
@Abyx:您的观点似乎是,不同的代码会产生不同的错误消息,那么不同的问题就不值得问了。我同意。
-
实际上是相同的代码,减少到错误消息更清晰的程度。只有有意义的行,
std::move替换为它的(预期的)实现。然后我们看到这样的std::move不能在那里使用,我们明白为什么它会退回到复制。
标签: c++ constructor constants move-semantics