vendredi 4 janvier 2019

perfect foward a parameter pack: convert args without expansion?

In unit tests, I'm trying to achieve a more functional code and to simplify a class of function calls like:

wchar_t message[100];
swprintf(message, 100, L"size: %d", size);
Logger::WriteMessage(message);

into:

Logger::WriteMessage(Message(L"size: %d", size));

using:

template<typename... T>
wchar_t* Message(T &&... args)
{
    wchar_t message[100];
    swprintf(message, 100, forward<T>(args));
    return message;
}

But I have

Error C2665 'swprintf': none of the 2 overloads could convert all the argument types

Error C3520 'args': parameter pack must be expanded in this context

1. Am I doing the right thing in the first place ? What do I miss ?

2. Ideally I would like to write something like (c#):

$"size:{size}".ToLogMessage();

I think I cannot in c++{11,14,17}. Do you confirm ?

Aucun commentaire:

Enregistrer un commentaire