简体   繁体   中英

Trying to use std::add_const to turn T& into const T&

I have a T& which has a const and non-const version of a function. I want to call the const version of the function. I try using std::add_const to turn T& into const T& but it doesn't work. What am I doing wrong and how can I fix it?

Here is a simple example.

void f(int&)
{
    std::cout << "int&" << std::endl;
}

void f(const int&)
{
    std::cout << "const int&" << std::endl;
}

int main()
{
    int a = 0;
    int& r = a;
    f(static_cast<std::add_const<decltype (r)>::type>(r));
}

Output: int&

Type traits are a very laborious way of approaching this. Simply use template type deduction:

void f(int&)
{
    std::cout << "int&" << std::endl;
}

void f(const int&)
{
    std::cout << "const int&" << std::endl;
}

template<typename T>
const T& make_const(T& t) { return t; }

int main()
{
    int a = 0;
    int& r = a;
    f(make_const(r));
}

References cannot be cv-qualified, so std::add_const applied to a reference type leaves it unchanged. In this particular case you should just do

f(static_cast<const int&>(r));

but in general you will have to transform the reference type T like this:

template<typename T>
struct add_const_to_reference {
    typedef T type;
};

template<typename T>
struct add_const_to_reference<T&> {
    typedef const T& type;
};

template<typename T>
struct add_const_to_reference<T&&> {
    typedef const T&& type;
};

std::add_const does nothing for a reference, since a reference can't be const qualified, only what it references can be.

In your case, a simple solution is to make a const version:

int main()
{
    int a = 0;
    int& r = a;
    const int &const_r = r;
    f(const_r);
}

or if you don't want to specify an explicit type:

int main()
{
    int a = 0;
    int& r = a;
    const auto &const_r = r;
    f(const_r);
}

If you just want to prevent treating an expression as mutable, you could use std::cref :

#include <functional>

int main()
{
    int a = 0;
    int& r = a;
    f(std::cref(r));
}

Here cref returns a std::reference_wrapper<const int> struct, which implicitly converts back to const int& .

If you want to actually do something with the corresponding reference-to-const-something type, either create your own trait as @Brian suggested, or use decltype on your own wrapper function as @BenVoigt suggested, or you could do something like this if you really still want to take advantage of cref :

int main()
{
    int a = 0;
    int& r = a;
    using ConstRefT = decltype(std::cref(r))::type &;
}

The general answer to how to do manual overload resolution is to cast the function pointer, ie here static_cast<(void(*)(const int&)>(f)(r) . Uhm, I hope I got the parentheses right. In this specific case you can alternatively just cast the argument, f( static_cast<const int&>( r ) ) , since static_cast can do any implicit conversion.

int main()
{
    int a = 0;
    int& r = a;
    f(static_cast<int const&>(r));
}

If you want to generalize that by composing the standard library's type modifiers, then you can do it like this:

f(static_cast<std::remove_reference<decltype(r)>::type const&>(r));

Since that's pretty ugly I recommend just defining a general const -adder, or for an ad-hoc thing declaring a reference to const locally and use that as argument.

A general const -adder can go like this:

template< class Type >
auto const_ref( Type const& r ) -> Type const& { return r; }

const_cast is what you need. See: http://www.cplusplus.com/doc/tutorial/typecasting/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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