[英]how to use function as literal string in C or C++ macro
我昨天發布了一個類似的問題 ,該網站建議發布一個新的問題,並提供更好的解釋。
有兩個宏:
#define COMPANY L"Test Company"
#define PRODUCT COMPANY L" in Canada"
PRODUCT的結果將是“加拿大測試公司”。
現在,我們有以下要求:
變更問題:PRODUCT的結果將是“ Test Company”,丟失了“ in Canada”部分。
這是代碼:
#include <stdio.h>
#include <tchar.h>
const wchar_t* getCompanyName() { return L"Test Company";};
#define COMPANY getCompanyName();
#define PRODUCT COMPANY L" in Canada"
int _tmain(int argc, _TCHAR* argv[])
{
const wchar_t * company = COMPANY; // get Test Company
const wchar_t * product = PRODUCT; // get Test Company in Canada
wprintf(company);
wprintf(product);
return 0;
}
您的PRODUCT
宏會展開
getCompanyName(); L" in Canada"
所以
const wchar_t * product = getCompanyName(); L" in Canada";
wprintf(product);
版畫
Test Company
如預期的那樣。
在C ++中,我們傾向於:
因此,在C ++中,我們更喜歡:
inline const std::wstring getCompanyName() { return L"Test Company";}
inline const std::wstring PRODUCT() { return getCompanyName() + L" in Canada";}
這是一個討厭的hack,但實際上是可能的。 將COMPANY
定義為以文字開頭,以文字結尾並可以隱式轉換為const wchar_t *
的表達式:
#define COMPANY L"" + getCompanyName() + L""
當然, getCompanyName()
一定不能返回const wchar_t *
,因為沒有為兩個指針定義operator+
,並且無論如何它都可以在地址而不是字符串上工作。
您基本上需要as std::wstring
,但是您可能需要將其轉換為const wchar_t *
,而std::wstring
則不是。 因此,您必須定義自己的類:
struct AutoConvertibleString {
std::string s;
AutoConvertibleString(const std::string &s) : s(s) {}
// C++ optimization for move:
// AutoConvertibleString(std::string s) : s(std::move(s)) {}
operator const wchar_t *() { return s.c_str(); }
};
AutoConvertibleString operator+(const wchar_t *l, const AutoConvertibleString &r) {
return (l + r.s).c_str();
}
AutoConvertibleString operator+(const AutoConvertibleString &l, const wchar_t *r) {
return (l.s + r).c_str();
}
// Ok, the operator+s could be optimized to use move for temporaries too...
AutoConvertibleString getCompanyName() { /* ... whatever ... */ }
這是一個丑陋的駭客。 將它們全部轉換為函數確實更好。 但是應該可以。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.