所以问题很简单,如果编译器能够根据标准推断出模板连接吗?
示例:
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
是否可以传递?
了解更多; Previous Question
答案 0 :(得分: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();
}
我知道。看起来不太漂亮。但是works on GCC.