Skip to content

<format>: dynamic width or precision of non-integral type should be rejected #2785

@mordante

Description

@mordante

Describe the bug
Per [format.string.std]/7

… If the corresponding formatting argument is not of integral type 
… an exception of type format_­error is thrown.

When using a non-integral type as arg-id in std::format the code violates [format.fmt.string]/3 and is therefore ill-formed. The STL incorrectly accepts the format string, however, and throws an exception at run-time https://godbolt.org/z/b868c3M8z.

#include <format>
#include <iostream>

int main() {
    try {
        std::cout << std::format("{:*^{}}\n", 'a', 1.1);
    } catch(...) {
        std::cout << "die\n";
    }
    try {
        std::cout << std::format("{:.{}}\n", "abc", 1.1);
    } catch(...) {
        std::cout << "die\n";
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedSomething works now, yay!formatC++20/23 format

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions