You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#include<fmt/core.h>
#include<fmt/color.h>intmain()
{
auto quality = [&] {
returnfmt::styled(std::string_view("great"), fmt::emphasis::bold);
};
auto quality2 = quality();
bool condition = false;
auto quality3 = [&] {
// Doesn't compile if this is uncommented.//if (condition) return fmt::styled("not great", fmt::emphasis::bold);returnfmt::styled("great", fmt::emphasis::bold);
}();
// Either of the following two crashes at runtimefmt::print("result: {}\n", quality());
//fmt::print("result: {}\n", quality2);// This works, but can only work for alternatives of the same length.fmt::print("result: {}\n", quality3);
}
I'm only using the string_view here because in the real world I use multiple strings of different length which end up having different types when styled and thus can't be returned from a function as for quality3. It would be great if this or something equivalent worked, nice if the limitations were documented, good if there were a compiler error and bad (for me) if I'm just too dumb. A workaround is to format the quality flag before storing it of course, but that means formatting twice.
The text was updated successfully, but these errors were encountered:
This crashes at runtime:
https://godbolt.org/z/3cq9eWhnM
I'm only using the
string_view
here because in the real world I use multiple strings of different length which end up having different types when styled and thus can't be returned from a function as forquality3
. It would be great if this or something equivalent worked, nice if the limitations were documented, good if there were a compiler error and bad (for me) if I'm just too dumb. A workaround is to format the quality flag before storing it of course, but that means formatting twice.The text was updated successfully, but these errors were encountered: