使 class 期望单个模板参数的模板模板参数接受可变模板参数的模板模板参数?
Make class expecting a template template parameter of single template parameter accept a template template parameter of variadic template parameter?
我正在尝试编写类似于 std::bind
的内容,但针对的是模板类型而不是对象。实际上,我想将一个模板参数绑定到一个模板类型。 (以一种类似的方式,在 std::bind
中,我们将一个(或多个)真实参数绑定到一个函数式对象。)
下面的 C++ 代码最能说明我想要什么:
#include <tuple>
#include <type_traits>
using namespace std;
/* This struct should be in some header-only utility library */
template <template <typename...> typename Template, typename T>
struct template_bind {
template <typename... Args>
using template_type = Template<T, Args...>;
};
/* This class should be in a separate header file */
// If we change the following line to add '...' then it works
// template <template <typename...> typename Template>
template <template <typename> typename Template>
class my_complicated_class {
public:
/* stuff */
private:
Template<float> data_;
};
/* This probably would be in a third file that includes both headers */
int main()
{
using t1 = template_bind<std::tuple, int>;
using t2 = template_bind<t1::template_type, double>;
my_complicated_class<t2::template_type> stuff_with_tuple; // Compile error
using p1 = template_bind<std::pair, int>;
my_complicated_class<p1::template_type> stuff_with_pair; // Compile error
}
有趣的是,代码可以在 GCC 和 MSVC 上使用 C++17 编译,但不能在 Clang(使用任何 C++ 标准)或 GCC/MSVC 上使用 C++14 编译。错误(在那些不会编译它的编译器上)是 my_complicated_class
需要一个接受 单个 模板参数的模板模板参数,但是 template_type
是可变模板。
更改 my_complicated_class
以接受可变模板模板参数可解决所有编译器上的问题。但是,将 my_complicated_class
更改为接受可变模板模板参数会感觉很奇怪,因为 my_complicated_class
可能不应该知道所使用的模板模板参数的任何信息。 (否则,可以提出一个论点,即所有模板模板参数都应该写成可变参数模板,例如 template <template <typename...> typename Template>
,但这似乎不是模板模板参数通常的写法。)
哪个编译器 is/are 不符合标准,我怎样才能使代码在 C++14 编译器上编译?
Clang 不支持 C++17 模板模板 argument/parameter 匹配。
相关段落:
A template-argument matches a template template-parameter (call it P) when each of the template parameters in the template-parameter-list of the template-argument's corresponding class template or alias template (call it A) matches the corresponding template parameter in the template-parameter-list of P. [...]
A template-argument matches a template template-parameter P when P is at least as specialized as the template-argument A. [...]
这是使您的代码在 C++17 中 well-formed 的最后一句话。当 P 是 template<class> class
且 A 是 template<class...> class
时,P 比 A 更专业。
我正在尝试编写类似于 std::bind
的内容,但针对的是模板类型而不是对象。实际上,我想将一个模板参数绑定到一个模板类型。 (以一种类似的方式,在 std::bind
中,我们将一个(或多个)真实参数绑定到一个函数式对象。)
下面的 C++ 代码最能说明我想要什么:
#include <tuple>
#include <type_traits>
using namespace std;
/* This struct should be in some header-only utility library */
template <template <typename...> typename Template, typename T>
struct template_bind {
template <typename... Args>
using template_type = Template<T, Args...>;
};
/* This class should be in a separate header file */
// If we change the following line to add '...' then it works
// template <template <typename...> typename Template>
template <template <typename> typename Template>
class my_complicated_class {
public:
/* stuff */
private:
Template<float> data_;
};
/* This probably would be in a third file that includes both headers */
int main()
{
using t1 = template_bind<std::tuple, int>;
using t2 = template_bind<t1::template_type, double>;
my_complicated_class<t2::template_type> stuff_with_tuple; // Compile error
using p1 = template_bind<std::pair, int>;
my_complicated_class<p1::template_type> stuff_with_pair; // Compile error
}
有趣的是,代码可以在 GCC 和 MSVC 上使用 C++17 编译,但不能在 Clang(使用任何 C++ 标准)或 GCC/MSVC 上使用 C++14 编译。错误(在那些不会编译它的编译器上)是 my_complicated_class
需要一个接受 单个 模板参数的模板模板参数,但是 template_type
是可变模板。
更改 my_complicated_class
以接受可变模板模板参数可解决所有编译器上的问题。但是,将 my_complicated_class
更改为接受可变模板模板参数会感觉很奇怪,因为 my_complicated_class
可能不应该知道所使用的模板模板参数的任何信息。 (否则,可以提出一个论点,即所有模板模板参数都应该写成可变参数模板,例如 template <template <typename...> typename Template>
,但这似乎不是模板模板参数通常的写法。)
哪个编译器 is/are 不符合标准,我怎样才能使代码在 C++14 编译器上编译?
Clang 不支持 C++17 模板模板 argument/parameter 匹配。
相关段落:
A template-argument matches a template template-parameter (call it P) when each of the template parameters in the template-parameter-list of the template-argument's corresponding class template or alias template (call it A) matches the corresponding template parameter in the template-parameter-list of P. [...]
A template-argument matches a template template-parameter P when P is at least as specialized as the template-argument A. [...]
这是使您的代码在 C++17 中 well-formed 的最后一句话。当 P 是 template<class> class
且 A 是 template<class...> class
时,P 比 A 更专业。