Skip to content

Instantly share code, notes, and snippets.

@osyo-manga
Created June 16, 2011 17:18
Show Gist options
  • Save osyo-manga/1029726 to your computer and use it in GitHub Desktop.
Save osyo-manga/1029726 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <boost/variant.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/mpl/print.hpp>
typedef boost::mpl::print<int>::type hogehoge;
typedef boost::variant<std::string const&, char const*> any_string;
struct c_str_impl{
typedef char const* result_type;
result_type
operator ()(any_string const& str) const{
return boost::apply_visitor(*this, str);
}
result_type
operator ()(std::string const& str) const{
return str.c_str();
}
result_type
operator ()(char const* str) const{
return str;
}
} c_str;
void
hoge(any_string v){
std::cout << c_str(v) << std::endl;
}
int
main(){
hoge("char*");
hoge(std::string("string"));
std::cout << "hello world" << std::endl;
std::cout << "hel" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment