The function template changes the parameter declared with the top level const: clang bug?

The code below compiles correctly on clang 3.8.1-1 in ArchLinux.

Is this a clang error?

gcc enter the correct warning / error.

 template <class T> struct BugReproducer{ using size_type = typename T::size_type; int bug1(size_type count); int bug2(size_type count) const; static int bug3(size_type count); }; template <class T> int BugReproducer<T>::bug1(size_type const count){ // this is a bug. must be not allowed count = 5; // return is to use the result... return count; } template <class T> int BugReproducer<T>::bug2(size_type const count) const{ // same for const method count = 5; return count; } template <class T> int BugReproducer<T>::bug3(size_type const count){ // same for static method count = 5; return count; } struct DummyVector{ using size_type = int; }; int main(){ using BugRepr = BugReproducer<DummyVector>; BugRepr reproducer; auto a = reproducer.bug1(1); auto b = reproducer.bug2(1); auto c = BugRepr::bug3(1); // return is to use the result... return a + b + c; } 

This is how I compile:

 [nmmm@zenbook HM3]$ clang x.cc -std=c++11 -lstdc++ -Wall -Wpedantic -Wconversion 

clang and c++14 are the same result.

 [nmmm@zenbook HM3]$ clang x.cc -std=c++14 -lstdc++ -Wall -Wpedantic -Wconversion 

Here is the gcc output:

 [nmmm@zenbook HM3]$ gcc x.cc -std=c++11 -lstdc++ -Wall -Wpedantic -Wconversion x.cc: In instantiation of 'int BugReproducer<T>::bug1(BugReproducer<T>::size_type) [with T = DummyVector; BugReproducer<T>::size_type = int]': x.cc:46:28: required from here x.cc:13:8: error: assignment of read-only parameter 'count' count = 5; ~~~~~~^~~ x.cc: In instantiation of 'int BugReproducer<T>::bug2(BugReproducer<T>::size_type) const [with T = DummyVector; BugReproducer<T>::size_type = int]': x.cc:47:28: required from here x.cc:22:8: error: assignment of read-only parameter 'count' count = 5; ~~~~~~^~~ x.cc: In instantiation of 'static int BugReproducer<T>::bug3(BugReproducer<T>::size_type) [with T = DummyVector; BugReproducer<T>::size_type = int]': x.cc:48:20: required from here x.cc:29:8: error: assignment of read-only parameter 'count' count = 5; ~~~~~~^~~ 
+8
c ++ c ++ 11 clang c ++ 14
source share
1 answer

Yes, this is a bug in clang; filed at https://llvm.org/bugs/show_bug.cgi?id=30365 .

The nature of the error lies in the fact that in defining the function of a member of the class template outside ( [class.mfct] / 1) the class template with the parameter type depending on the parameter class template, clang uses the type of the declaration parameter, not the type of the definition parameter in which they differ in the highest cv qualification. A simplified example:

 template<class T> struct A { void f(typename T::U); }; template<class T> void A<T>::f(typename T::U const i) { i = 1; } struct X { using U = int; }; int main() { A<X>{}.f(0); } 

Per [dcl.fct] / 5 type i in the definition of A<X>::f is int const ( Using 'const' for function parameters ):

5 - [...] After creating a list of parameter types, any top-level cv-qualifiers that change the type parameter are deleted when the function type is formed. [...] [Note. This conversion does not affect parameter types. [...] - end note]

+5
source share

All Articles