如何在不重复其签名的情况下强制模板化函数的特定实例化? [英] How do I force a specific instantiation of a templated function without repeating its signature?

查看:233
本文介绍了如何在不重复其签名的情况下强制模板化函数的特定实例化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于某些特定的模板参数,我需要实例化具有长签名的模板化函数foo().

I need to instantiate a templated function foo() with a long signature, for some specific template arguments.

我只是阅读了此问题的答案,该答案本质上是建议复制函数签名但设置特定参数.我想以某种方式避免这种情况.有什么合理的方法来实现这一目标?例如可以让我写东西的东西

I just read the answers to this question which essentially suggest copying the function signature but setting the specific arguments. I want to avoid that somehow. What is a reasonable way to achieve this? e.g. something which would allow me to write

INSTANTIATE(foo, template_arg1, template_arg2);

或者也许

MyFunctionType<template_arg1, template_arg2> foo;

仅出于说明目的,假设这是foo的代码:

Just for illustrative purposes, suppose this is the code for foo:

template<typename T, int val>
unsigned foo(
    T bar,
    SomeType baz1,
    SomeOtherType baz2,
    YetAnotherType you_catch_the_drift) 
{ 
    /* some code here */ 
}

推荐答案

您可以简单地定义一个宏:

You can simply define a macro:

#define INSTANTIATE_FOO(T, val) \
template \
unsigned foo<T, val>( \
    T bar, \
    SomeType baz1, \
    SomeOtherType baz2, \
    YetAnotherType you_catch_the_drift);

并以以下方式使用它:

INSTANTIATE_FOO(int, 0)

这篇关于如何在不重复其签名的情况下强制模板化函数的特定实例化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆