【发布时间】:2021-04-08 22:48:09
【问题描述】:
正如标题所说,我正在尝试创建一些要使用的东西:
template <typename T>
void testFunc(int& i)
{
...
}
int i { 0 };
ForEach<int, float>::run<testFunc>(i);
我已经尝试了一些东西,但我遇到了一些问题:
template<typename CurrentComponentType, typename... ComponentTypes>
struct ForEach
{
template<void (&func)(auto&&... args)>
static constexpr void run(auto&&... args)
{
func<CurrentComponentType>(std::forward<decltype(args)>(args)...);
ForEach<ComponentTypes...>::run<func>(std::forward<decltype(args)>(args)...);
}
};
template<typename CurrentComponentType>
struct ForEach<CurrentComponentType>
{
template<void (&func)(auto&&... args)>
static constexpr void run(auto&&... args)
{
func<CurrentComponentType>(std::forward<decltype(args)>(args)...);
}
};
-
我不知道如何将模板函数作为(模板但不一定)参数。
-
由于某些我不明白的原因,我无法再次调用
run()函数:run<func>(。上面写着'<unresolved overloaded function type>'。
我认为有很多事情我不明白。
我该如何解决它,为什么它不能以这种方式工作?我误会了什么?
【问题讨论】:
-
testFunc不是函数,它是模板函数,但run()的模板参数是函数而不是模板函数。 -
@PatrickRoberts 确实解释了第二个问题......但我不知道如何解决它......
-
在 C++20 中你可以做到this,你可以用 lambdas 代替模板函数吗?
-
@PatrickRoberts 我是,我只是不知道有什么区别
-
一个 lambda(即使带有模板参数)是一个值,而模板函数是一个模板值。 C++ 不支持非类型模板模板参数。
标签: c++ template-meta-programming