Skip to content

Instantly share code, notes, and snippets.

@usagi
Created July 24, 2016 14:05
Show Gist options
  • Save usagi/147693a1f70e8fd703f99e9e52d3fb21 to your computer and use it in GitHub Desktop.
Save usagi/147693a1f70e8fd703f99e9e52d3fb21 to your computer and use it in GitHub Desktop.
C++14で引数の型の順序と数を任意にする方法 ref: http://qiita.com/usagi/items/5df0ed07b5807a985131
parameter_x_type x;
parameter_y_type y;
parameter_z_type z;
parameter_w_type w;
f( x, y, z, w );
auto f
( const parameter_x& x
, const parameter_y& y
, const parameter_z& z
, const parameter_w& w
) { /* なにかしらの処理 */ }
f( x );
f( y, w );
f( z, w, x );
f( x, y, w, z);
/* ... その他の組み合わせのみなさん ... */
#include <boost/optional.hpp>
#include <vector>
#include <string>
#include <iostream>
namespace some_library
{
using boost::optional;
template < typename T > struct has_vector_type { using vector_type = std::vector< T >; };
struct area_type { float a, b, c, d; };
struct tag_type : has_vector_type< tag_type > { std::string value; tag_type( const std::string& in ) : value( in ) { } };
struct keyword_type : has_vector_type< keyword_type > { std::string value; keyword_type( const std::string& in ) : value( in ) { } };
struct location_type : has_vector_type< tag_type > { };
// for library internal implements
namespace detail
{
struct f_state { };
auto f( const f_state& state )
{
std::cout << "c(execute state)";
/* TODO: ここで state から適切な処理を行い結果を return */
return location_type::vector_type{ };
}
template < typename T, typename ... TS >
auto f( const f_state& state, const T& value, TS ... ts )
{
std::cout << "b:0(configure state) -> ";
/* TODO: ここで T value を state へ適用 */;
return f( state, ts ... );
}
template < typename ... TS >
auto f( const f_state& state, const area_type& value, TS ... ts )
{
std::cout << "b:1(configure state by area_type) -> ";
/* TODO: */;
return f( state, ts ... ); }
}
// for library user
template < typename T, typename ... TS >
auto f( const T& value, TS ... ts )
{
std::cout << "a(make state) -> "; return detail::f( { }, value, ts ... );
}
}
auto main() -> int
{
using namespace some_library;
area_type area { 0, 1, 2, 3 };
tag_type::vector_type tags { { "aaa" }, { "bbb" }, { "ccc" } };
keyword_type::vector_type keywords { { "hoge" }, { "fuga" }, { "piyo" } };
std::cout << "1: ";
f( area );
std::cout << "\n";
std::cout << "2: ";
f( area, tags );
std::cout << "\n";
std::cout << "3: ";
f( area, tags, keywords );
std::cout << "\n";
std::cout << "4: ";
f( tags, keywords, area );
std::cout << "\n";
}
1: a(make state) -> b:1(configure state by area_type) -> c(execute state)
2: a(make state) -> b:1(configure state by area_type) -> b:0(configure state) -> c(execute state)
3: a(make state) -> b:1(configure state by area_type) -> b:0(configure state) -> b:0(configure state) -> c(execute state)
4: a(make state) -> b:0(configure state) -> b:0(configure state) -> b:1(configure state by area_type) -> c(execute state)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment