C++ cli C++/CLI语法澄清

C++ cli C++/CLI语法澄清,c++-cli,C++ Cli,在查看marshal_时,函数调用之一需要以下格式: System::String^ const & 托管指针后面的常量&的用途是什么 这不适合我: static std::wstring GetString(const System::String^ value) { return msclr::interop::marshal_as<std::wstring>(value); } static std::wstring GetString(const Syst

在查看marshal_时,函数调用之一需要以下格式:

System::String^ const &
托管指针后面的
常量&
的用途是什么

这不适合我:

static std::wstring GetString(const System::String^ value)
{
    return msclr::interop::marshal_as<std::wstring>(value);
}
static std::wstring GetString(const System::String^value)
{
返回msclr::interop::marshal_as(值);
}
错误1错误C4996:

“msclr::interop::error_reporting_helper::marshal_as”:库不支持此转换,或者不包括此转换所需的头文件。请参阅有关“如何:扩展封送处理库”的文档,以添加您自己的封送处理方法。c:\ProgramFiles(x86)\microsoft visual studio 9.0\vc\include\msclr\marshal.h 203

这是:

static std::wstring GetString(const System::String^ value)
{
    return msclr::interop::marshal_as<std::wstring>(const_cast<System::String^>(value));
}
static std::wstring GetString(const System::String^value)
{
返回msclr::interop::marshal_as(const_cast(value));
}

封送处理方法由以下模板定义:

template <class _To_Type, class _From_Type>
inline _To_Type marshal_as(const _From_Type&);
为什么编译器不能进行转换?请注意,
String^const&
const String^const&
不是一回事。第一个是“指向字符串对象的常量指针”。第二个表示“指向常量字符串对象的常量指针”。在使用
(const System::String^value)
的方法中,您有一个常量字符串对象,您调用的函数需要一个指向非常量字符串对象的指针。我们知道String类是不可变的,但编译器不是,因此它不会将常量对象的地址传递给认为该对象是非常量的方法


(我使用的是Visual Studio 2010 SP1,但我不认为封送处理方法在2008年和2010年之间有任何重大变化。)

我只是问,因为我有一个函数,我写了一个const System::String^并将其转换为std::wstring,而函数原型不喜欢没有const_cast。你能展示更多关于const_cast的代码吗?我无法重新创建该错误:我尝试将一个方法声明为采用
const String^
,我可以不使用const_cast调用它。我修改了原始问题,以包括非工作示例和工作示例。如果重要的话,我使用的是Visual Studio 2008 SP1。我只需要从本地方法声明中删除
常量。String类是不可变的,它不需要
const
保护。看编辑啊。。明白了。不过,在我使用助手方法的地方,我破坏了所有调用场景。废话。:)
static std::wstring GetString(System::String^ value)
{
    return msclr::interop::marshal_as<std::wstring>(value);
}