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

gcc 11, C++20: error: 'f' is not a constant expression #2753

Closed
D-r-P-3-p-p-3-r opened this issue Feb 10, 2022 · 5 comments
Closed

gcc 11, C++20: error: 'f' is not a constant expression #2753

D-r-P-3-p-p-3-r opened this issue Feb 10, 2022 · 5 comments
Labels

Comments

@D-r-P-3-p-p-3-r
Copy link

Under Linux with gcc 11 the following example builds fine with -std=c++17 but fails to build with -std=c++20:

#include <iostream>
#include "fmt/format.h"

template<typename... Args>
void foo(const char * const f, Args... args){
	const auto bar = fmt::format(f, args...);
	std::cout << bar << std::endl;
}

int main() {
	foo("test {} {}", 1, 2);
}

libfmt 8.0.1 and 8.1.1:

main.cpp: In instantiation of 'void foo(const char*, Args ...) [with Args = {int, int}]':
main.cpp:11:5: required from here
main.cpp:6:30: in 'constexpr' expansion of 'fmt::v8::basic_format_string<char, int&, int&>(f)'
main.cpp:6:37: error: 'f' is not a constant expression
6 | const auto bar = fmt::format(f, args...);
| ~~~~~~~~~~~^~~~~~~~~~~~

Why?

@vitaut
Copy link
Contributor

vitaut commented Feb 10, 2022

The format string must be known at compile time or wrapped in fmt::runtime.

@antonysigma
Copy link

antonysigma commented Mar 7, 2022

I also encounter this problem with g++-11. Perhaps this issue is a duplicate of #2455 (comment) ?

The format string must be known at compile time or wrapped in fmt::runtime.

I thought std::string_view is always a constant and known at compile time? This is my pseudo code:

#include <iostream>
#include "fmt/format.h"

template<typename... Args>
void foo(std::string_view f, Args... args){
	const auto bar = fmt::format(f, args...);
	std::cout << bar << std::endl;
}

int main() {
	foo("test {} {}", 1, 2);
}

Update: I am forcing runtime for the time being, until this issue is resolved. #2438

@vitaut
Copy link
Contributor

vitaut commented Mar 7, 2022

I thought std::string_view is always a constant and known at compile time?

It is not.

@antonysigma
Copy link

Update: Turns out if I am willing to give up const char* or std::string_view as function parameters, I can enable compile-time format string checking. https://stackoverflow.com/a/69647103

@vitaut Sorry for posting new messages to the "closed" issue.

@vitaut
Copy link
Contributor

vitaut commented Mar 8, 2022

No worries, glad you've found the solution.

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

3 participants