Anonymous
Как передать аргумент шаблона в std::function?
Сообщение
Anonymous » 18 дек 2024, 10:01
У меня есть несколько шаблонных функций для объединения вектора в строку в моем проекте C++. И мне нужно передать алгоритм агрегации из одной функции в другую, используя std::function.
Код: Выделить всё
template
requires (is_convertible_v || is_convertible_v || is_convertible_v || is_convertible_v || is_convertible_v)
LPCWSTR StringHelper::ImplodeVectorToStringW(vector vectorValues, LPCWSTR lpcwszSeparator)
{
wstring wstrSeparator(lpcwszSeparator);
function fn = [&](wstring total, T current) -> wstring { return total + lpcwszSeparator + ToWString(current); };
wstring wstrResult = Aggregate(vectorValues, wstring(L""), fn);
LPCWSTR lpcwszResult = GetStringBufferW(wstrResult);
return lpcwszResult;
}
template
basic_string Aggregate(vector vectorValues, basic_string initValue, function fnReduce)
{
basic_string total = initValue;
for_each(vectorValues.begin(), vectorValues.end(), [&](TValue current) { fnReduce(total, current); });
return total;
}
template
requires (is_convertible_v || is_convertible_v || is_convertible_v || is_convertible_v || is_convertible_v)
wstring StringHelper::ToWString(T Value)
{
wstring wstrResult;
if constexpr(is_convertible_v)
{
wstrResult = to_wstring((BYTE)Value);
}
else if constexpr(is_convertible_v)
{
wstrResult = AnsiToUnicode(string(1, (CHAR)Value).c_str());
}
else if constexpr(is_convertible_v)
{
wstrResult = wstring(1, (WCHAR)Value);
}
else if constexpr(is_convertible_v)
{
wstrResult = AnsiToUnicode((LPCSTR)Value);
}
else if constexpr(is_convertible_v)
{
wstrResult = (LPCWSTR)Value;
}
return wstrResult;
}
LPCWSTR StringHelper::AnsiToUnicode(LPCSTR lpcszValue)
{
LPWSTR lpwszResult = NULL;
if(!IsEmptyStringA(lpcszValue))
{
string strValue(lpcszValue);
int size = MultiByteToWideChar(CP_THREAD_ACP, 0, strValue.c_str(), -1, NULL, 0);
if(size > 0)
{
lpwszResult = new WCHAR[size];
if(!(MultiByteToWideChar(CP_THREAD_ACP, 0, strValue.c_str(), -1, lpwszResult, size) && lpwszResult != NULL))
{
delete[] lpwszResult;
lpwszResult = NULL;
}
}
}
return lpwszResult;
}
LPWSTR StringHelper::GetStringBufferW(LPCWSTR lpcwszValue)
{
LPWSTR lpwszBuffer = NULL;
if(lpcwszValue != NULL)
{
size_t size = wcslen(lpcwszValue) + 1;
lpwszBuffer = new WCHAR[size];
wcscpy_s(lpwszBuffer, size, lpcwszValue);
}
return lpwszBuffer;
}
LPWSTR StringHelper::GetStringBufferW(wstring wstrValue)
{
return GetStringBufferW(wstrValue.c_str());
}
Но когда я отлаживаю этот код, функция fn инициализируется следующим значением:
Нет требуемого лямбда-выражения. Что не так?
Подробнее здесь:
https://stackoverflow.com/questions/792 ... tdfunction
1734505280
Anonymous
У меня есть несколько шаблонных функций для объединения вектора в строку в моем проекте C++. И мне нужно передать алгоритм агрегации из одной функции в другую, используя std::function. [code] template requires (is_convertible_v || is_convertible_v || is_convertible_v || is_convertible_v || is_convertible_v) LPCWSTR StringHelper::ImplodeVectorToStringW(vector vectorValues, LPCWSTR lpcwszSeparator) { wstring wstrSeparator(lpcwszSeparator); function fn = [&](wstring total, T current) -> wstring { return total + lpcwszSeparator + ToWString(current); }; wstring wstrResult = Aggregate(vectorValues, wstring(L""), fn); LPCWSTR lpcwszResult = GetStringBufferW(wstrResult); return lpcwszResult; } template basic_string Aggregate(vector vectorValues, basic_string initValue, function fnReduce) { basic_string total = initValue; for_each(vectorValues.begin(), vectorValues.end(), [&](TValue current) { fnReduce(total, current); }); return total; } template requires (is_convertible_v || is_convertible_v || is_convertible_v || is_convertible_v || is_convertible_v) wstring StringHelper::ToWString(T Value) { wstring wstrResult; if constexpr(is_convertible_v) { wstrResult = to_wstring((BYTE)Value); } else if constexpr(is_convertible_v) { wstrResult = AnsiToUnicode(string(1, (CHAR)Value).c_str()); } else if constexpr(is_convertible_v) { wstrResult = wstring(1, (WCHAR)Value); } else if constexpr(is_convertible_v) { wstrResult = AnsiToUnicode((LPCSTR)Value); } else if constexpr(is_convertible_v) { wstrResult = (LPCWSTR)Value; } return wstrResult; } LPCWSTR StringHelper::AnsiToUnicode(LPCSTR lpcszValue) { LPWSTR lpwszResult = NULL; if(!IsEmptyStringA(lpcszValue)) { string strValue(lpcszValue); int size = MultiByteToWideChar(CP_THREAD_ACP, 0, strValue.c_str(), -1, NULL, 0); if(size > 0) { lpwszResult = new WCHAR[size]; if(!(MultiByteToWideChar(CP_THREAD_ACP, 0, strValue.c_str(), -1, lpwszResult, size) && lpwszResult != NULL)) { delete[] lpwszResult; lpwszResult = NULL; } } } return lpwszResult; } LPWSTR StringHelper::GetStringBufferW(LPCWSTR lpcwszValue) { LPWSTR lpwszBuffer = NULL; if(lpcwszValue != NULL) { size_t size = wcslen(lpcwszValue) + 1; lpwszBuffer = new WCHAR[size]; wcscpy_s(lpwszBuffer, size, lpcwszValue); } return lpwszBuffer; } LPWSTR StringHelper::GetStringBufferW(wstring wstrValue) { return GetStringBufferW(wstrValue.c_str()); } [/code] Но когда я отлаживаю этот код, функция fn инициализируется следующим значением: [code]fn = wstrSeparator = L" "; [/code] Нет требуемого лямбда-выражения. Что не так? Подробнее здесь: [url]https://stackoverflow.com/questions/79287396/how-to-pass-template-argument-to-stdfunction[/url]