Overly Verbose Error Message for Invalid STL Container Type (e.g., std::vector<int&>)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Dear GCC Team,

I would like to bring to your attention an issue regarding the verbosity of
error messages in G++ when dealing with an invalid use of reference types
in STL containers.

Consider the following minimal example:

c++
CopyEdit
#include <vector>void do_something() {
    std::vector<int&> v;
    int j = 20;
    v.push_back(j);
    // Irrelevant code omitted.
}

This code is *invalid by the C++ standard*, as std::vector<T> requires the
type T to be *CopyConstructible* and *Assignable*. Since int& is not an
object type and does not satisfy these requirements, this usage is
incorrect.

However, the compiler error produced by G++ is extremely verbose —
spanning *over
200 lines* in some cases. It delves into internal template instantiations
and allocator machinery, with messages like the inability to create _Tp*
from int& inside new_allocator.h, followed by a cascade of
unrelated-looking errors.

Compare this to the following unrelated but invalid code:

c++
CopyEdit
#include <iostream>int main() {
    int j = 20;
    std::cout << j+* << std::endl;
    return 0;
}

This results in a clear and concise message:

CopyEdit
$ g++ wrong.cpp
wrong.cpp: In function ‘int main()’:
wrong.cpp:4:22: error: expected primary-expression before ‘<<’ token

*Suggestion:* Could the diagnostic for invalid container types like
std::vector<int&> be made more user-friendly, perhaps by detecting
unsupported reference types early in template instantiation and emitting a
targeted error such as:

error: std::vector<T> cannot be instantiated with T = int& because
reference types are not valid container elements

This would greatly improve usability and reduce confusion, especially for
learners or those debugging larger codebases.

I’m happy to provide the full error log if that helps.

Best regards,
*Abdulrehman Ansari*




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux