这更像是一个概念性问题.我正在尝试找到将两个arg模板(参数类型)转换为一个arg模板的最简单方法.即,绑定其中一种类型.
这将是bindboost/std中的元编程等价物.我的例子包括一个可能的用例,即std::is_same作为模板参数传递给一个模板,该模板采用一个arg模板模板参数(std::is_same是一个双arg模板),即TypeList::FindIf.在TypeList没有完全在这里实现,也不是FindIf,但你的想法.它采用"一元谓词"并返回该谓词为真的void类型,如果不是这样的类型.
我有两个工作变体,但第一个不是单行,第二个使用相当冗长的BindFirst装置,这对非类型模板参数不起作用.有没有一种简单的方法来编写这样的单行程?我相信我正在寻找的程序被称为currying.
#include <iostream>
template<template<typename, typename> class Function, typename FirstArg>
struct BindFirst
{
template<typename SecondArg>
using Result = Function<FirstArg, SecondArg>;
};
//template<typename Type> using IsInt = BindFirst<_EqualTypes, int>::Result<Type>;
template<typename Type> using IsInt = std::is_same<int, Type>;
struct TypeList
{
template<template<typename> class Predicate>
struct FindIf
{
// this needs to be implemented, return void for now
typedef void Result;
};
};
int main() …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
template<typename T>
struct A { };
// same as A, but with one extra defaulted parameter
template<typename T, typename F = int>
struct B { };
template<template<typename> typename T>
T<int> build() { return {}; }
int main()
{
build<A>(); // works in gcc and clang
build<B>(); // works in gcc, does not work in clang
}
Run Code Online (Sandbox Code Playgroud)
g ++(7.3.0)编译代码就好了,但是,clang ++(5.0.1)会发出以下命令:
example.cpp:14:5: error: no matching function for call to 'build'
build<B>(); // works in gcc, does not work in clang
^~~~~~~~ …Run Code Online (Sandbox Code Playgroud)