Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22000

Const correctness in automatic template deduction

$
0
0

In the following example I would expect the line a.foo(j); to pass the const qualifier to the templated method.

#include <iostream>
#include <type_traits>

struct A
{
    template<typename T>
    void foo(T i) const
    {
        std::cout << "Is const type: "<< std::is_const<T>::value << "\n";
    }
};

int main() {

    A a;

    int i = 0;
    const int j = 0;

    a.foo(i);
    a.foo(j);

    a.foo<decltype(i)>(i);
    a.foo<decltype(j)>(j);

    a.foo<int>(i);
    a.foo<const int>(j);

    return 0;
}

However the output I get both from gcc and clang (c++17) is the following.

Is const type: 0
Is const type: 0
Is const type: 0
Is const type: 1
Is const type: 0
Is const type: 1

The second row is false instead of true. So why automatic template deduction drops cv qualifiers? Is any specific reason why this happened?

PS. The example above can be found here


Viewing all articles
Browse latest Browse all 22000

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>