https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123104
Bug ID: 123104
Summary: wrongly reported code line for a compiler error
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: ing.russomauro at gmail dot com
Target Milestone: ---
With the following code, that contains twice the same expression in two
different overloaded template functions,
the compiler indicates the wrong code line index for an error.
The correct line is shown if the expression in the non-selected overload
syntactically changes.
https://godbolt.org/z/1EsK4cMrd
#include <type_traits>
#include <vector>
template<typename T>
requires requires(T v){v.size(); v[0];}
auto func(const T&){
//error wrongly appears here, but not if '[1]' is used instead of '[0]',
//that is, with a statement different from the really erroneous
//one in the below overload.
using V = std::decay_t<decltype(std::declval<T>()[0])>;
return V{};
};
template<typename T>
auto func(const std::vector<T>&){
//Error here, should avoid subscript '[0]'
//but the error is reported as if it is in the overload above,
//likely due to the same statement.
using V = std::decay_t<decltype(std::declval<T>()[0])>;
return V{};
};
int main(){
std::vector<int> v;
func(v); //selects the instantiation with std::vector
}