Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing constructors with fixed parameters and constructors with constructor templates

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

My Code:

#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?

like image 380
gartenriese Avatar asked Jan 12 '23 13:01

gartenriese


1 Answers

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

Test t(std::string("Hello"), 42, 0.07f);
like image 187
Kerrek SB Avatar answered Jan 16 '23 17:01

Kerrek SB