可变参数模板的歧义-空参数包

时间:2019-05-09 15:19:47

标签: c++

如何消除此代码的歧义? (Try it here

active?: true

这是我找到的解决方案……但我对此并不满意。 (Try it here

template <typename T>
inline void f()
{   
}

template<typename T, typename ... Args>
inline void f()     
{
    f<Args...>();
}

int main() 
{
    f<char, double, int>();     

    return 0;
}

是否可以为空参数包提供模板专业化?

2 个答案:

答案 0 :(得分:3)

您可以做的是向可变参数重载中添加另一个模板参数,例如

template<typename T, typename U, typename ... Args>
inline void f()     
{
    f<U, Args...>();
}

此方法只能使用2个或更多参数来调用该函数,因此在单个参数或单个参数与空包之间不再存在歧义。


如果您可以使用C ++ 17,则根据功能要实现的目标,您也可以切换为使用fold expression

答案 1 :(得分:1)

您根本没有专门研究Parameters for each reg model are: Intercept 14.991641 B 0.229818 C 0.000553 dtype: float64 The summary of regressionis: OLS Regression Results ============================================================================== Dep. Variable: A R-squared: 0.453 Model: OLS Adj. R-squared: -0.095 Method: Least Squares F-statistic: 0.8270 Date: Thu, 09 May 2019 Prob (F-statistic): 0.547 Time: 11:41:30 Log-Likelihood: -19.963 No. Observations: 5 AIC: 45.93 Df Residuals: 2 BIC: 44.76 Df Model: 2 Covariance Type: nonrobust ============================================================================== coef std err t P>|t| [0.025 0.975] ------------------------------------------------------------------------------ Intercept 14.9916 25.386 0.591 0.615 -94.234 124.217 B 0.2298 0.927 0.248 0.827 -3.760 4.219 C 0.0006 0.001 0.717 0.548 -0.003 0.004 ============================================================================== Omnibus: nan Durbin-Watson: 1.562 Prob(Omnibus): nan Jarque-Bera (JB): 0.477 Skew: -0.145 Prob(JB): 0.788 Kurtosis: 1.515 Cond. No. 5.21e+04 ============================================================================== Warnings: [1] Standard Errors assume that the covariance matrix of the errors is correctly specified. [2] The condition number is large, 5.21e+04. This might indicate that there are strong multicollinearity or other numerical problems. Parameters for each reg model are: Intercept 7.389442 B 0.739083 C 0.000133 dtype: float64 The summary of regressionis: OLS Regression Results ============================================================================== Dep. Variable: A R-squared: 0.712 Model: OLS Adj. R-squared: 0.424 Method: Least Squares F-statistic: 2.473 Date: Thu, 09 May 2019 Prob (F-statistic): 0.288 Time: 11:41:30 Log-Likelihood: -18.208 No. Observations: 5 AIC: 42.42 Df Residuals: 2 BIC: 41.24 Df Model: 2 Covariance Type: nonrobust ============================================================================== coef std err t P>|t| [0.025 0.975] ------------------------------------------------------------------------------ Intercept 7.3894 12.545 0.589 0.616 -46.589 61.368 B 0.7391 0.545 1.357 0.308 -1.604 3.082 C 0.0001 0.001 0.240 0.833 -0.002 0.003 ============================================================================== Omnibus: nan Durbin-Watson: 1.522 Prob(Omnibus): nan Jarque-Bera (JB): 0.285 Skew: 0.387 Prob(JB): 0.867 Kurtosis: 2.122 Cond. No. 3.66e+04 ============================================================================== !您已经超负荷了。也就是说,您有两个名称为f的模板,并且要使用哪个模板是模棱两可的。考虑呼叫f。您是要使用f<int>()实例化第一个模板,还是要使用T = int实例化第二个模板?如果您想适当地专门化T = int; Args = {},我认为实际上需要将其包装到一个类中(以允许部分专门化):

f

然后您可以使用以下语法对其进行专门化:

template<typename T, typename... Args>
struct f_wrapper
{
    static inline void f()
    {
        f_wrapper<Args...>::f();
    }
};

现在,有一个名为template<typename T> // the brackets v v distinguish specialization from overloading // you can't overload classes anyway, but it's important for functions struct f_wrapper<T> { static inline void f() { } }; 的类模板,它具有两个特长。 f中没有歧义。唯一的模板用f_wrapper<int>::f()实例化,然后用于选择T = int; Args = {}专业化(第二个定义)。

然后您可以继续将其包装:

Args = {}