混合具有固定参数的构造函数和具有构造函数模板的构造函数 [英] Mixing constructors with fixed parameters and constructors with constructor templates

查看:159
本文介绍了混合具有固定参数的构造函数和具有构造函数模板的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将构造函数与固定参数和构造函数模板混合?

Is it possible to mix constructors with fixed parameters and constructor templates?

我的代码:

#include <iostream>

class Test {
    public:
        Test(std::string, int, float) {
            std::cout << "normal constructor!" << std::endl;
        }

        template<typename ... Tn>
        Test(Tn ... args) {
            std::cout << "template constructor!" << std::endl;
        }
};

int main() {
    Test t("Hello World!", 42, 0.07f);
    return 0;
}

这给了我模板构造函数!有没有办法,我的正常构造函数被调用?

This gives me "template constructor!". Is there a way, that my normal constructor is called?

推荐答案

当然,如果两个同样好的匹配, -template是首选:

Sure, in the event of two equally good matches, the non-template is preferred:

Test t(std::string("Hello"), 42, 0.07f);

这篇关于混合具有固定参数的构造函数和具有构造函数模板的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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