add_const or remove_const based on another type Here is a type trait which - I feel - could fit in std next to std::add_const and std::remove_const:

#include &lt;type_traits&gt;

template &lt;typename T&gt;
struct set_constness_of { 
    template &lt;typename U&gt;
    struct by {
        using type = typename std::conditional&lt;
            std::is_const&lt;U&gt;::value,
            typename std::add_const&lt;T&gt;::type,
            typename std::remove_const&lt;T&gt;::type
            &gt;::type;
    };
};


Note this is intentionally C++11 to minimize requirements for using it. One can obviously define set_constness_of_t in which by is a type rather than a struct with a type.

I've also written a tiny sample program (coliru.com) to ensure it runs, at least for some cases. 

/* the above definition of set_constness_of goes here (paste it) */
#inlcude &lt;iostream&gt;  

struct foo {
    void m() { std::cout &lt;&lt; "Non-const\n"; }
    void m() const { std::cout &lt;&lt; "Const\n"; }
};

template &lt;class T&gt; void call_m() { T().m(); }

int main() {
    call_m&lt;foo&gt;();
    using bar = const int;
    call_m&lt; set_constness_of&lt;foo&gt;::by&lt;bar&gt;::type &gt;();
}


Questions:


Is this correctly defined for all cases?
What do you think about placing the second template parameter as an inner struct's template param, rather than having two template parameters on the outer struct?
What do you think about the choice of naming?


Other comments are welcome.
I don't see any obvious omissions or bugs.

The naming and inner struct are neatly expressive (and remind me of some testing frameworks' precondition/assertion chaining).  It's not something that's done by the standard library, but I don't think it should be hugely controversial.

I might go with a naming like copy_const&lt;foo&gt;::from&lt;bar&gt; - or perhaps even the other way around: copy_const&lt;bar&gt;::to&lt;foo&gt;.  Or even both!  (Actually, I now wish std::is_assignable worked like that - it would be easier to remember which parameter is which!)

None of the above is a concrete suggestion - intended merely as food for thought!