C++隐藏模板参数

lig*_*ulb 2 c++ templates

我不知道这是否可行,但我想"隐藏"给定类的一些模板参数.这就是我的意思,说我有以下代码:

template<class A, int b>
class Foo
{
};
template<template<class,int> class Foo_specialized, class A, int b>
class Bar
{
    Foo_specialized<A,b> obj;
};
Run Code Online (Sandbox Code Playgroud)

现在假设Bar不需要知道A,但需要了解b.当然这样的事情是完美的(以下是伪代码,只是为了说明这个想法):

template<template<int> class Foo_specialized_by_first_parameter, int b>
class Bar
{
    Foo_specialized_by_first_parameter<b> obj;
};
Run Code Online (Sandbox Code Playgroud)

我不确定这是否有可能,这个想法是在实例化Bar时有这样的东西:

Bar<Foo<float>, 5> bar_instance;
Run Code Online (Sandbox Code Playgroud)

当然这不起作用,因为Foo不接受1个参数.基本上我需要一些(Foo<float>)<5>可能的东西.我能想到的最接近的事情是在哈斯克尔中讨论.

Jar*_*d42 5

您可以使用模板typedef:

template <int N>
using Foo_float = Foo<float, N>;
Run Code Online (Sandbox Code Playgroud)

然后,与

template <template<int> class Foo_specialized_by_first_parameter, int b>
class Bar
{
    Foo_specialized_by_first_parameter<b> obj;
};
Run Code Online (Sandbox Code Playgroud)

你可能会这样做:

Bar<Foo_float, 5> bar_instance;
Run Code Online (Sandbox Code Playgroud)