我根本不知道这是否可行,但我想隐藏"来自给定类的一些模板参数。这就是我的意思,说我有以下代码:
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;
};
现在假设Bar不需要知道A,但需要了解b。 当然这样的事情是完美的(以下是用于说明这个想法的伪代码):
template<template<int> class Foo_specialized_by_first_parameter, int b>
class Bar
{
Foo_specialized_by_first_parameter<b> obj;
};
我不确定这是否可能,这个想法是在实例化时有这样的事情Bar:
Bar<Foo<float>, 5> bar_instance;
当然这不起作用,因为Foo不接受1个参数。
基本上我需要(Foo<float>)<5>
这样的东西才有可能。我能想到的最接近的事情是在哈斯克尔中讨论。
答案 0 :(得分:5)
您可以使用模板typedef:
template <int N>
using Foo_float = Foo<float, N>;
然后,用
template <template<int> class Foo_specialized_by_first_parameter, int b>
class Bar
{
Foo_specialized_by_first_parameter<b> obj;
};
你可能会这样做:
Bar<Foo_float, 5> bar_instance;
答案 1 :(得分:1)
假设您可以将该int更改为std::integral_constant
:
#include <iostream>
#include <string>
#include <map>
template<template<typename...> typename T, typename H>
struct Bind1st
{
template<typename... Arg>
using type = T<H, Arg...>;
};
int main() {
// to bind it
Bind1st< std::map, std::string >::type< std::string > mymap;
mymap[ "a" ] = "b";
}
当然,Bar< Bind1st< Foo, float >::type, 5 >
也应该有用。