有没有办法写一个BSTR字面量? [英] Is there a way to write a BSTR literal?
问题描述
当调用一个需要BSTR的函数时,可以这样写:
iFoo- > function(bsHELLO);然而,我知道的唯一解决方法是使用一个包装器调用 > SysAllocString
等,例如: iFoo-> function(WideString(LHELLO ).c_bstr());
这是一种丑陋。是否有这样的选项来创建 BSTR
字面量?
动机:更容易阅读的代码,通过避免分配和释放更快的运行时性能。
澄清:我只是谈到调用者(即我们)拥有BSTR的所有权的情况,例如:调用一个 BSTR [in]
参数。当然,为一个函数提供一个指向BSTR字面的指针是很愚蠢的,这个函数将继续尝试并释放字符串。
解决方案要跟进@ MSalters的回答,自定义用户定义的文本可能看起来像这样:
CComBSTR运算符_bstr(const char * str,std :: size_t len)
{
return CComBSTR(len,str);
}
然后你可以这样做( CComBSTR 具有定义的 BSTR
转换运算符):
iFoo-> function(HELLO_bstr);
您甚至可以为多个输入字符串文字类型重载运算符:
CComBSTR operator_bstr(const wchar_t * str,std :: size_t len)
{
return CComBSTR(len,str);
}
CComBSTR运算符_bstr(const char16_t * str,std :: size_t len)
{
return CComBSTR(len,(wchar_t *)str) ;
}
iFoo-> function(LHELLO_bstr); // call wchar_t * version with UTF-16 encoded data
iFoo-> function(uHELLO_bstr); // call char16_t * version with UTF-16 encoded data
iFoo-> function(u8HELLO_bstr); // call char * version with UTF-8 encoded data ...
注意最后一种情况。由于操作符不知道是否正在传递ANSI或UTF-8数据,并且 CComBSTR
传递 char *
数据,应该使用不同的字面后缀进行区分,以便可以正确转换UTF-8,例如:
CComBSTR operator_utf8bstr(const char * str,std :: size_t len)
{
std :: wstring_convert< std :: codecvt_utf8_utf16< wchar_t>,wchar_t>转换
std :: wstring wstr = conv.from_bytes(std :: string(str,len));
return CComBSTR(wstr.length(),wstr.c_str());
}
iFoo-> function(u8HELLO_utf8bstr);
When calling a function that expects a BSTR it'd be nice to be able to write something like:
iFoo->function( bs"HELLO" );
However the only workaround I'm aware of is to use a wrapper that calls SysAllocString
etc., e.g.:
iFoo->function( WideString(L"HELLO").c_bstr() );
which is kind of ugly. Is there actually such an option to create a BSTR
literal?
Motivation: easier-to-read code, and faster runtime performance by avoiding an allocation and deallocation.
Clarification: I am only talking about situations where the caller (i.e. us) has ownership of the BSTR, for example: calling a function that takes a BSTR [in]
parameter. Of course, it would be silly to supply a pointer to a BSTR literal to a function which will go on to try and free the string.
解决方案 To follow up on @MSalters's answer, a custom user-defined literal could look something like this:
CComBSTR operator "" _bstr (const char* str, std::size_t len)
{
return CComBSTR(len, str);
}
Then you can do this (as CComBSTR
has a BSTR
conversion operator defined):
iFoo->function( "HELLO"_bstr );
You can even overload the operator for multiple input string literal types:
CComBSTR operator "" _bstr (const wchar_t* str, std::size_t len)
{
return CComBSTR(len, str);
}
CComBSTR operator "" _bstr (const char16_t* str, std::size_t len)
{
return CComBSTR(len, (wchar_t*)str);
}
iFoo->function( L"HELLO"_bstr ); // calls wchar_t* version with UTF-16 encoded data
iFoo->function( u"HELLO"_bstr ); // calls char16_t* version with UTF-16 encoded data
iFoo->function( u8"HELLO"_bstr ); // calls char* version with UTF-8 encoded data...
Note the last case. Since the operator will not know whether it is being passed ANSI or UTF-8 data, and CComBSTR
assumes ANSI when passed char*
data, you should use a different literal suffix to differentiate so you can convert the UTF-8 correctly, eg:
CComBSTR operator "" _utf8bstr (const char* str, std::size_t len)
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> conv;
std::wstring wstr = conv.from_bytes(std::string(str, len));
return CComBSTR(wstr.length(), wstr.c_str());
}
iFoo->function( u8"HELLO"_utf8bstr );
这篇关于有没有办法写一个BSTR字面量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!