Skip to content

Instantly share code, notes, and snippets.

@RDCH106
Created April 15, 2019 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RDCH106/f7b7d07220bc239d2c5cd5d364c1901e to your computer and use it in GitHub Desktop.
Save RDCH106/f7b7d07220bc239d2c5cd5d364c1901e to your computer and use it in GitHub Desktop.
Explicit directive simulation for method arguments and avoid implicit conversion
#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