Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consteval error on C++20 #2438

Closed
TheCompez opened this issue Jul 28, 2021 · 2 comments
Closed

Consteval error on C++20 #2438

TheCompez opened this issue Jul 28, 2021 · 2 comments
Labels

Comments

@TheCompez
Copy link

TheCompez commented Jul 28, 2021

Hi,
As you know we can't use constexpr variables in function parameter.
But I need use function parameters from other json files.

Example :

std::string functionTest(std::string_view content, const std::string& value) {
  return fmt::format(content, value);//Error on compile time :)
}
std::cout << functionTest("The answer is {number}",  10);

How can I solve this problem?

Error:

error: call to consteval function 'fmt::basic_format_string<char, std::string>::basic_format_string<std::string, 0>' is not a constant expression
error: call to consteval function 'fmt::basic_format_string<char, std::string>::basic_format_string<std::string, 0>' is not a constant expression
  return fmt::format(text, val);
                     ^
note: non-literal type 'std::string' (aka 'basic_string<char>') cannot be used in a constant expression
@vitaut
Copy link
Contributor

vitaut commented Jul 28, 2021

You can wrap the format string in fmt::runtime to disable compile-time checks:

std::string functionTest(std::string_view content, const std::string& value) {
  return fmt::format(fmt::runtime(content), value);
}

or pass it as format_string as {fmt} does.

@TheCompez
Copy link
Author

You can wrap the format string in fmt::runtime to disable compile-time checks:

std::string functionTest(std::string_view content, const std::string& value) {
  return fmt::format(fmt::runtime(content), value);
}

or pass it as format_string as {fmt} does.

Thanks for your attention! @vitaut :)

facebook-github-bot pushed a commit to facebook/sapling that referenced this issue Apr 21, 2022
Summary:
When we try to switch to platform 10 we get this compile error:

```
eden/fs/nfs/Nfsd3.cpp:1378:26:
error: call to consteval function
'fmt::basic_format_string<char, const unsigned int &>::basic_format_string<const char *, 0>'
is not a constant expression
  return fmt::format(fmtString, std::get<typename T::TrueVariant>(val.v));
```

The problem is that fmtString either needs to be a `basic_format_string` or
needs to be a constexpr so that we can cast it to `basic_format_string` through
various means.

Not totally sure why this worked pre c++ 20, but C++20 introduced some stricter
type checking on the fmt library or constexpr stuff in general i guess.

fmtlib/fmt#2438 outlines the possible fixes

`fmt::runtime` looks like the best way.

Reviewed By: vitaut

Differential Revision: D35756658

fbshipit-source-id: 7b65ccf0719a964ab1f2fc1aed72744e72374d20
Toni500github added a commit to BurntRanch/TabAUR that referenced this issue Apr 22, 2024
And so bring the C++20 designated-initializers.
it was fault of log_print{f,ln} all-along
fixed thanks to fmtlib/fmt#2438
nevack added a commit to nevack/transmission that referenced this issue May 7, 2024
fmt::format_string ctor is consteval with C++20
See fmtlib/fmt#2438

Signed-off-by: Dzmitry Neviadomski <[email protected]>
nevack added a commit to nevack/transmission that referenced this issue May 22, 2024
fmt::format_string ctor is consteval with C++20
See fmtlib/fmt#2438

Signed-off-by: Dzmitry Neviadomski <[email protected]>
nevack added a commit to nevack/transmission that referenced this issue May 22, 2024
fmt::format_string ctor is consteval with C++20
See fmtlib/fmt#2438

Signed-off-by: Dzmitry Neviadomski <[email protected]>
nevack added a commit to nevack/transmission that referenced this issue May 22, 2024
fmt::format_string ctor is consteval with C++20
See fmtlib/fmt#2438

Signed-off-by: Dzmitry Neviadomski <[email protected]>
nevack added a commit to nevack/transmission that referenced this issue May 22, 2024
fmt::format_string ctor is consteval with C++20
See fmtlib/fmt#2438

Signed-off-by: Dzmitry Neviadomski <[email protected]>
nevack added a commit to nevack/transmission that referenced this issue Jun 28, 2024
fmt::format_string ctor is consteval with C++20
See fmtlib/fmt#2438

Signed-off-by: Dzmitry Neviadomski <[email protected]>
nevack added a commit to nevack/transmission that referenced this issue Jun 29, 2024
… and cli

fmt::format_string ctor is consteval with C++20
See fmtlib/fmt#2438

Signed-off-by: Dzmitry Neviadomski <[email protected]>
nevack added a commit to nevack/transmission that referenced this issue Jun 29, 2024
… and cli

fmt::format_string ctor is consteval with C++20
See fmtlib/fmt#2438

Signed-off-by: Dzmitry Neviadomski <[email protected]>
nevack added a commit to nevack/transmission that referenced this issue Oct 29, 2024
… and cli

fmt::format_string ctor is consteval with C++20
See fmtlib/fmt#2438

Signed-off-by: Dzmitry Neviadomski <[email protected]>
nevack added a commit to nevack/transmission that referenced this issue Dec 29, 2024
… and cli

fmt::format_string ctor is consteval with C++20
See fmtlib/fmt#2438

Signed-off-by: Dzmitry Neviadomski <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants