1

所以问题很简单,编译器是否应该能够根据标准推断模板连接?

例子:

template<typename... RUN_TIME, typename T, typename... CONSTRUCTION_TIME> 
Runnable<T, RUN_TIME...>* makeRunnable(T (*FunctionType)(CONSTRUCTION_TIME..., RUN_TIME...), CONSTRUCTION_TIME... ct_args)
{
    ...
}

int print_function(char arg1, int arg2, const char* arg3);

makeRunnable<const char*>(print_function, 'C', -3);

print_function给定参数和显式模板,编译器是否应该能够确定该函数可以通过?

看更多; 上一个问题

4

1 回答 1

0

除了标准说明编译器应该能够做什么的问题之外,这里有一些 TMP 可以使它在 gcc 上编译:

/** PackDiff: 
 *  Template Metafunction to deduce the difference of two parameter packs.
 *  To distinguish two packs we have to brace them in two tuples
 */
template <class Tup1, class Tup2> struct PackDiff;

/** Basic algorithm: (T, X1...) - (T, X2...) = (X1...) - (X2...) */
template <class T, class... Pack1, class... Pack2>
struct PackDiff<std::tuple<T, Pack1...>, std::tuple<T, Pack2...>>
  : PackDiff<std::tuple<Pack1...>, std::tuple<Pack2...>> {};

/** End of recursion: (X1...) - () = (X1...) */
template <class... Pack1>
struct PackDiff<std::tuple<Pack1...>, std::tuple<>>
{
  typedef std::tuple<Pack1...> type;
};

/** Mismatch: (T, X1...) - (U, X2...) is undefined */
template <class T1, class... Pack1, class T2, class... Pack2>
struct PackDiff<std::tuple<T1, Pack1...>, std::tuple<T2, Pack2...>>
{ typedef struct PACK_MISMATCH {} type; };

/** Rest: () - (X2...) is undefined */  
template <class... Pack2>
struct PackDiff<std::tuple<>, std::tuple<Pack2...>>
{ typedef struct LEFT_PACK_TOO_SHORT{} type; };



//make Runnable Type of T and a diff tuple:
template <class T, class Tup1> struct MakeRunnableType;

template <class T, class... Pack>
struct MakeRunnableType<T, std::tuple<Pack...>>
{ typedef Runnable<T, Pack...> type; };



template<typename... AllArgs, typename T, typename... SomeArgs> 
auto makeRunnable(T (*FunctionType)(AllArgs...), SomeArgs... ct_args)
  -> typename MakeRunnableType<T, 
          typename PackDiff<std::tuple<AllArgs...>, 
                std::tuple<SomeArgs...>
          >::type
     >::type*
{
  typedef typename PackDiff<std::tuple<AllArgs...>, 
                std::tuple<SomeArgs...>
          >::type PackDiff_t;
  typedef typename MakeRunnableType<T, PackDiff_t>::type ReturnType;
  return new ReturnType();
}

我知道。看起来不是很漂亮。但适用于 GCC。

于 2013-03-07T15:45:37.707 回答