I'm trying to somehow disable/mark as deprecated the hideous std::string::operator=(char)
overload (which in my experience is used only when mistakingly assigning an integer to a string, and causes subtle and difficult to track bugs).
I tried with:
an explicit specialization with a static assert in it
#include <string> #include <type_traits> template<> std::basic_string<char> &std::basic_string<char>::operator=(char c) { static_assert(false, "Don't use this!"); }
which fails as
<string>
already does an explicit instantiation ofstd::string
- the
[[deprecated]]
attribute, applied to a similar declaration as above in various positions; no position I tried seemed to yield any reasonable result; =delete
, which fails for reasons similar to above;- I thought about using linker tricks (in a similar vein, in the same project we have runtime checks on stray
setlocale
usages using the--wrap
ld
linker option), but the fact that this is a template and inline method complicates the matter.
Now to the questions:
- is there a standard method to somehow disable (as would happen with
=delete
) any function or method in the standard library (read: in a library where you cannot alter the declarations in the headers)? - as above, but, instead of disable, add a warning (as would happen with
[[deprecated]]
); - failing the standard method, is there something g++-specific?
- if there's no "general" (=applicable to any method, any class, any function, ...) solution, is there something that we could apply to this specific case (=disable a method of a template class, possibly even just a specific instantiation)?