简体   繁体   English

函数模板修改使用顶级const声明的参数:clang bug?

[英]Function template modifies parameter declared with top-level const: clang bug?

The code below compiles correctly on clang 3.8.1-1 on ArchLinux. 下面的代码clang 3.8.1-1在ArchLinux的clang 3.8.1-1上正确编译。

Is this clang bug? 这是clang虫吗?

gcc issue correct warning/error on this. gcc对此发出正确的警告/错误。

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;
}

Here how I compile: 这里是我的编译方式:

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

clang and c++14 - same result. clangc++14相同的结果。

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

Here is gcc output: 这是gcc输出:

[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;
  ~~~~~~^~~

Yes, this is a bug in clang; 是的,这是Clang中的错误; filed at https://llvm.org/bugs/show_bug.cgi?id=30365 . 归档于https://llvm.org/bugs/show_bug.cgi?id=30365

The nature of the bug is that in a class template member function definition appearing outside ( [class.mfct] /1) the class template, with the type of a parameter dependent on the class template parameters, clang uses the parameter type of the declaration rather than the parameter type of the definition where they differ in topmost cv-qualification. 该错误的本质是,在类模板成员函数定义出现在类模板之外( [class.mfct] / 1)时,参数的类型取决于类模板参数,clang使用声明的参数类型而不是定义的参数类型,它们的最高简历要求不同。 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 the type of i within the definition of A<X>::f is int const ( Use of 'const' for function parameters ): [dcl.fct] / 5在A<X>::f定义内的i的类型为int const对函数参数使用'const' ):

5 - [...] After producing the list of parameter types, any top-level cv-qualifiers modifying a parameter type are deleted when forming the function type. 5-[...]生成参数类型列表后,在形成函数类型时会删除所有修改参数类型的顶级cv限定词 [...] [ Note: This transformation does not affect the types of the parameters. [...] [ 注意:此转换不会影响参数的类型。 [...] — end note ] [...] — 尾注 ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM