Могу ли я создать новую строку во время компиляции и использовать во время выполнения?C++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 Могу ли я создать новую строку во время компиляции и использовать во время выполнения?

Сообщение Anonymous »

Я пишу функцию журнала, которая принимает строку формата, и переменное количество аргументов и отражает std :: source_location . Это не просто, поэтому я попробовал FormatWithLocation , описанное в том, как использовать Source_location в функции переменного шаблона? - Что работает. И я на самом деле не вижу способа создать строку времени компиляции и использовать ее во время выполнения без жесткой кодирования строки. Я прибегал к кучу хитростей, включая использование Std :: Array для строк времени компиляции, но для тех, мне нужно знать размер заранее; При любой другой структуре данных я не могу использовать данные с размышлениями о куче компиляции в мире времени выполнения (понятно). < /p>

Код: Выделить всё

#include 
#include 
#include 
#include 

// TODO: avoid hardcoded size
constexpr size_t N = 61;

struct StringWithSourceLocation {
template 
static consteval std::array toArray(
const char* const inputText) {
std::array array{};
for (size_t i = 0; i < inputLength; i++) array[i] = inputText[i];
return array;
}

template 
static consteval size_t computeLength(
const std::array& inputText) {
return inputLength * 2 + 1;
}

template 
static consteval std::array produceOutput(
const std::array& inputText) {
std::array outputText{};
size_t outputIndex = 0;
for (const char c : inputText) {
outputText[outputIndex++] = c;
outputText[outputIndex++] = c;
}
outputText[outputIndex++] = 0;
return outputText;
}

static consteval std::array process(
const std::source_location sourceLocation) {
// TODO: work without quotation marks
constexpr const char* functionName = "sourceLocation.function_name()";
constexpr size_t inputLength =
std::string::traits_type::length(functionName);

constexpr auto input = toArray(functionName);
constexpr size_t outputLength = computeLength(input);

constexpr std::array outputText =
produceOutput(input);
return outputText;
}

consteval StringWithSourceLocation(
const char* message,
const std::source_location sourceLocation =
std::source_location::current(),
const std::array processedOutputName =
process(std::source_location::current()))
: message(message),
sourceLocation(sourceLocation),
processedFunctionName(processedOutputName) {}

const char* message;
const std::source_location sourceLocation;
const std::array processedFunctionName;
};

static void log(const StringWithSourceLocation foo) {
// This shall return a compile-time result:
std::cout 

Подробнее здесь: [url]https://stackoverflow.com/questions/79656381/can-i-create-a-new-string-at-compile-time-and-use-at-runtime[/url]
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»