How to disappear completely RSSフィード

2011-07-08

Template引数に関数のシグネチャを渡した時の各パラメータ

Templateの引数関数シグネチャとか関数型を渡したときに、戻り値とか引数とかをそれぞれ取得する方法を知らなかった。取得する方法というかそのもととなる方法というか。

template<typename Signature>
struct func_sig;

template<typename R, typename A1, typename A2>
struct func_sig<R(A1, A2)>
{
    typedef R result_type;
    typedef A1 arg1_type;
    typedef A2 arg2_type;
};

#include <string>
#include <vector>

#include <boost/type_traits.hpp>
#include <boost/static_assert.hpp>

int main()
{
    typedef void foo_t(std::string, std::vector<int>);

    BOOST_STATIC_ASSERT((boost::is_same<func_sig<int(char, double)>::result_type, int>::value));
    BOOST_STATIC_ASSERT((boost::is_same<func_sig<int(char, double)>::arg1_type, char>::value));
    BOOST_STATIC_ASSERT((boost::is_same<func_sig<int(char, double)>::arg2_type, double>::value));

    BOOST_STATIC_ASSERT((boost::is_same<func_sig<foo_t>::result_type, void>::value));
    BOOST_STATIC_ASSERT((boost::is_same<func_sig<foo_t>::arg1_type, std::string>::value));
    BOOST_STATIC_ASSERT((boost::is_same<func_sig<foo_t>::arg2_type, std::vector<int> >::value));
}

こんなふうにすればよかったのか。。。

参考

FlastFlast 2011/07/09 16:01 もしかして:boost/type_traits/function_traits.hpp
http://boost.org/doc/libs/1_46_1/libs/type_traits/doc/html/boost_typetraits/reference/function_traits.html

heisseswasserheisseswasser 2011/07/10 00:59 そういうのがあるのは知ってて使ってもいたのですが、どういう仕組みで実現してるか知らなかったんですよね〜。でも提示してもらったそのsource読んだら一目瞭然過ぎて1年生からやり直したくなりました。

スパム対策のためのダミーです。もし見えても何も入力しないでください
ゲスト


画像認証

トラックバック - http://d.hatena.ne.jp/heisseswasser/20110708/1310109429