Skip to content

Compilation failure when passing std::ranges::cbegin to a function with std::ranges::const_iterator_t as a template parameter #169503

@redraincatching

Description

@redraincatching

Attempting to pass a std::ranges::const_iterator_t as a template parameter fails when std::ranges::cbegin is passed to the function.

#include <iterator>
#include <ranges>
#include <string>

template<std::input_iterator IT_T>
void some_function(IT_T)
{

}

int main(void) {
   std::string someString;
   some_function<std::ranges::const_iterator_t<std::string>>(std::ranges::cbegin(someString));
}

causes the compiler to fail with the following output:

<source>:13:4: error: no matching function for call to 'some_function'
   13 |    some_function<std::ranges::const_iterator_t<std::string>>(std::ranges::cbegin(someString));
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:6:6: note: candidate function template not viable: no known conversion from 'const_iterator_t<decltype(__r)>' to 'const_iterator_t<std::string>' for 1st argument
    6 | void some_function(IT_T)
      |      ^             ~~~~
1 error generated.
Compiler returned: 1

note that adding const to the variable and the const_iterator_t type results in the program compiling as expected

#include <iterator>
#include <ranges>
#include <string>

template<std::input_iterator IT_T>
void some_function(IT_T)
{

}

int main(void) {
   std::string const someString;
   some_function<std::ranges::const_iterator_t<std::string const&>>(std::ranges::cbegin(someString));
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    libstdc++GNU libstdc++ C++ standard librarylwg-issuethis is an issue that was filed to the Library Working Group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions