vendredi 29 mars 2019

C++ Deduced conflicting types in template pack with reference

I'm working on a program with following structure:

#include <iostream>
#include <string>

void fun(const std::string &text, int a, int b) { // (1)
    std::cout << text << a + b << std::endl;
}

template<typename ...Args>
void execute(void(*fun)(Args...), Args ...args) {
    fun(args...);
}

void init(const std::string &text, int a, int b) {
    execute(fun, text, a, b);
}

int main() {
    init("Fun: ", 1, 2);
    return 0;
}

and I get the error message

.code.tio.cpp:14:2: error: no matching function for call to 'execute'
        execute(fun, text, a, b);
        ^~~~~~~
.code.tio.cpp:9:6: note: candidate template ignored: deduced conflicting types for parameter 'Args' (<const std::__cxx11::basic_string<char> &, int, int> vs. <std::__cxx11::basic_string<char>, int, int>)
void execute(void(*fun)(Args...), Args ...args) {
     ^
1 error generated.

I can fix the error by removing the reference in line (1):

void fun(const std::string text, int a, int b) {

but I want to pass the values by reference and not by value. The function template

template<typename ...Args>
void execute(void(*fun)(Args...), Args ...args)

must not be changed. How can I fix this so that text is passed by reference, execute is not changed and init is also not changed if possible?

Aucun commentaire:

Enregistrer un commentaire