【板名*】プログラム技術
【スレ名*】C++相談室 part55
【スレのURL*】http://pc10.2ch.net/test/read.cgi/tech/1164951094/l50
【名前欄】
【メール欄】sage
【本文*】↓
operator ->*のほうがスマートでは、と思ったが、いざやってみたらBoostが要るわ(TR1でもいいが)。
まあ趣味ならBoostも気兼ねなく使えるだろう。
#include <iostream>
#include <string>
#include <cassert>
#include <boost/function.hpp>
#include <boost/bind.hpp>
std::string do_func(const std::string& str) {return "[" + str + "]";}
enum func_type {
&nbsp; func
};
boost::function<std::string ()> operator->*(const std::string& str, func_type fn) {
&nbsp; switch (fn) {
&nbsp; case func:
&nbsp; &nbsp; return boost::bind(do_func, str);
&nbsp; default:
&nbsp; &nbsp; assert(0);
&nbsp; &nbsp; return boost::function<std::string ()>();
&nbsp; }
}
int main() {
&nbsp; std::string str = "STRING";
&nbsp; std::cout << (str->*func)() << std::endl;
}