Is there any way to disable implicit conversion to void*
for pointers to a certain class?
No, you can't prevent the implicit conversion, but you could wrap the API function(s) in proxy functions that checks types at compile time and approve/disapprove them there.
Example:
#include <iostream>
#include <string>
#include <type_traits>
void api(void* p) { // your original API
std::cout << "void* " << p << '
';
}
template<class T>
void api_wrapper(T* p) { // your wrapper
// let constness fail in the original api instead of in the static_assert:
using type = std::remove_const_t<T>*;
static_assert(
// add your approved types here
std::is_convertible_v<type, std::ostream*> ||
std::is_convertible_v<type, std::string*>,
"Not an approved type"
);
api(p);
}
int main() {
std::string foo;
api_wrapper(&std::cout);
api_wrapper(&foo);
//api_wrapper(&std::cin); // compile time error "Not an approved type"
}
If the set of pointer types that you would like to disapprove is very small, then instead of listing all the approved types in the static_assert
, just list the disapproved types and adjust the boolean logic:
static_assert(
// add your disapproved types here
not std::is_convertible_v<type, std::ostream*> &&
not std::is_convertible_v<type, std::string*>,
"Not an approved type"
);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…