Created
April 15, 2019 11:09
-
-
Save RDCH106/f7b7d07220bc239d2c5cd5d364c1901e to your computer and use it in GitHub Desktop.
Explicit directive simulation for method arguments and avoid implicit conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <type_traits> | |
struct A | |
{ | |
operator const char*() { | |
return m_str.c_str(); | |
} | |
std::string m_str; | |
}; | |
template<typename T> | |
void f(const T *str) { | |
static_assert(std::is_same<T, char>::value, "str must be char*"); | |
} | |
int main() | |
{ | |
A a; | |
const char *pa = nullptr; | |
f(a); // Fails | |
f(pa); // Works | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment