是否可以用C++编写自定义转换运算符(如“static_cast”)
Is it possible to write custom cast operators in C++ (like `static_cast`)?
我得到了这个想法,并尝试编写一个string_cast
强制转换运算符来在C++字符串之间强制转换。
template <class OutputCharType>
class string_cast
{
public:
template <class InputCharType>
operator std::basic_string<OutputCharType>(const std::basic_string<InputCharType> & InputString)
{
std::basic_string<OutputCharType> OutputString;
const std::basic_string<InputCharType>::size_type LENGTH = InputString.length();
OutputString.resize(LENGTH);
for (std::basic_string<OutputCharType>::size_type i=0; i<LENGTH; i++)
{
OutputString[i] = static_cast<OutputCharType>(OutputString[i]);
}
return OutputString;
}
};
我试着这样使用它:
std::string AString("Hello world!");
std::cout << AString << std::endl;
std::wcout << string_cast<wchar_t>(AString) << std::endl; // ERROR
错误消息为:
Error C2440 '<function-style-cast>': cannot convert from
'const std::string' to 'string_cast<wchar_t>'
这在C++中是不可能的,还是我的代码中缺少了什么?
您可以编写带有签名的免费函数:
template <typename OutputCharType, typename InputCharType>
std::basic_string<OutputCharType>
string_cast(const std::basic_string<InputCharType>& InputString)
看起来你只想要一个非成员函数,而不是一个函子:
template <class OutputCharType, class InputCharType>
std::basic_string<OutputCharType>
string_cast(const std::basic_string<InputCharType> & InputString)
{
std::basic_string<OutputCharType> OutputString;
const auto LENGTH = InputString.length();
OutputString.resize(LENGTH);
for (auto i=0; i<LENGTH; i++)
{
OutputString[i] = static_cast<OutputCharType>(OutputString[i]);
}
return OutputString;
}
还要注意,我已将size_type
类型更改为auto
。这是因为它们是依赖名称,所以您需要使用typename
将它们用作类型(MSVC可能会让您不用它,但这是不可移植的):
const std::basic_string<InputCharType>::size_type LENGTH = InputString.length();
//change to
const typename std::basic_string<InputCharType>::size_type LENGTH = InputString.length();
// ^^^^^^^^
这会变得非常难看,所以最好只使用auto
。如果您不能使用C++11,您可以创建InSize
和OutSize
typedef
s。
虽然我同意Jarod42的答案是更好的方法。出现错误的原因是,您试图在没有对象的情况下使用转换运算符。
string_cast<wchar_t>(AString)
是试图调用接受std::string&
的c'tor。您的代码片段需要以下语法。
string_cast<wchar_t>()(AString)
注意额外的一对()
,它是对象创建。
相关文章:
- 如何理解C++标准N3337中的expr.const.cast子句8
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- C++Cast运算符过载
- 如何处理 c++ 中类实现中的"invalid use of non-static data member"?
- 收到错误"invalid use of non-static data member 'stu::n' "
- LNK1104:无法打开libpjproject-i386-Win32-vc14-Debug-Static.lib
- 我应该在 C++ 中何时/为什么使用 STATIC?
- 在VS2019项目中集成ImageMagick:x64-windows-static library
- 如何处理Boost Spirit X3导致Visual Studio 2019 "static initialization order fiasco"?
- "static char __ = []() -> char"的含义
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- 无法在 DLL 中链接 SDL2-static.lib
- 如何摆脱C++中未解析的外部符号"private: static char"错误?
- 错误:"cast"未命名类型void setCastDescription(std::string
- 通过使用 const-cast 的非常量引用来延长临时的寿命
- C++线程"Call to non-static member function without an object argument"
- 出现这种错误的原因是什么"invalid use of non-static data member "
- "(void) cast"与功能有什么区别 "__attributes__"来沉默未使用的参数警告?
- static是如何使用ClassA::m_variable处理所有类对象的
- 在[expr.static.cast]/4中,术语"一个可行函数"指的是什么