【问题标题】:Baffling variadic templates exercise莫名其妙的可变参数模板练习
【发布时间】:2021-08-19 03:24:29
【问题描述】:

我给自己设置了这个任务来帮助学习可变参数模板。函数add_and_cat() 应该首先采用一对,然后是可变数量的整数或字符串或混合。当它递归地遇到这些时,它应该从pair.first中减去每个int,并将每个字符串连接到pair.second。函数 pp 只是打印这对。

它似乎在许多情况下都有效,但在其他情况下,我得到一个编译错误,声称没有匹配函数(在底部)......尽管据我所知似乎只有一个完全明显的候选者:/我玩过并试图推理出来,但......还没有运气。我错过了什么?

-------------- 代码:-------- ---------

#include <iostream>

void pp(std::pair<int,std::string> printme) {
    std::cout << printme.first << " : " << printme.second << "\n";
}

//base case int
//must be int or std::string for now
void add_and_cat(std::pair<int, std::string>& store, int i) {
    store.first -= i;
}

//base case string
void add_and_cat(std::pair<int, std::string>& store, std::string s) {
    store.second += s;
}

//full case for int
template<typename ...Ts>
void add_and_cat(std::pair<int, std::string>& store, int i, Ts... rest) {
    store.first -= i;
    add_and_cat(store, rest...);
}

//full case for string
template<typename ...Ts>
void add_and_cat(std::pair<int, std::string>& store, std::string s, Ts... rest) {
    store.second += s;
    add_and_cat(store, rest...);
}



int main()
{

    std::pair<int, std::string> p{0,"START"};

    //add_and_cat(p, 1, 2, 3, 4); pp(p);                    //fine
    //add_and_cat(p, 3, 4, 5, 6); pp(p);                    //fine
    //add_and_cat(p, "A", "B", "C", "D"); pp(p);            //fine
    //add_and_cat(p, "D", "E", "F", "G"); pp(p);            //fine
    //add_and_cat(p, "A", 1, "B"); pp(p);                   //fine
    //add_and_cat(p, 1, "A", 1, "B"); pp(p);                //compile error
    //add_and_cat(p, 1, 2, "A",3); pp(p);                   //compile error
    //add_and_cat(p, "A", 1, 2, "B"); pp(p);                //fine
    //add_and_cat(p, "A", 1, 2, "B","C"); pp(p);            //compile error
    //add_and_cat(p, 1, 2, "B","C"); pp(p);             //compile error



    return 0;
}

------------------ 错误:--------------- --------------

/mnt/c/Users/Tim/Nextcloud/playground/seqan3/source/hello_world.cpp: In instantiation of ‘std::pair<int, std::__cxx11::basic_string<char> > add_and_cat(std::pair<int, std::__cxx11::basic_string<char> >&, int, Ts ...) [with Ts = {const char*, int, const char*}]’:
/mnt/c/Users/Tim/Nextcloud/playground/seqan3/source/hello_world.cpp:273:45:   required from here
/mnt/c/Users/Tim/Nextcloud/playground/seqan3/source/hello_world.cpp:250:20: error: no matching function for call to ‘add_and_cat(std::pair<int, std::__cxx11::basic_string<char> >&, const char*&, int&, const char*&)’
  250 |  return add_and_cat(store, rest...);
      |         ~~~~~~~~~~~^~~~~~~~~~~~~~~~
/mnt/c/Users/Tim/Nextcloud/playground/seqan3/source/hello_world.cpp:233:29: note: candidate: ‘std::pair<int, std::__cxx11::basic_string<char> > add_and_cat(std::pair<int, std::__cxx11::basic_string<char> >&, int)’
  233 | std::pair<int, std::string> add_and_cat(std::pair<int, std::string>& store, int i) {
      |                             ^~~~~~~~~~~
/mnt/c/Users/Tim/Nextcloud/playground/seqan3/source/hello_world.cpp:233:29: note:   candidate expects 2 arguments, 4 provided
/mnt/c/Users/Tim/Nextcloud/playground/seqan3/source/hello_world.cpp:240:29: note: candidate: ‘std::pair<int, std::__cxx11::basic_string<char> > add_and_cat(std::pair<int, std::__cxx11::basic_string<char> >&, std::string)’
  240 | std::pair<int, std::string> add_and_cat(std::pair<int, std::string>& store, std::string s) {
      |                             ^~~~~~~~~~~
/mnt/c/Users/Tim/Nextcloud/playground/seqan3/source/hello_world.cpp:240:29: note:   candidate expects 2 arguments, 4 provided
/mnt/c/Users/Tim/Nextcloud/playground/seqan3/source/hello_world.cpp:247:29: note: candidate: ‘template<class ... Ts> std::pair<int, std::__cxx11::basic_string<char> > add_and_cat(std::pair<int, std::__cxx11::basic_string<char> >&, int, Ts ...)’
  247 | std::pair<int, std::string> add_and_cat(std::pair<int, std::string>& store, int i, Ts... rest) {
      |                             ^~~~~~~~~~~
/mnt/c/Users/Tim/Nextcloud/playground/seqan3/source/hello_world.cpp:247:29: note:   template argument deduction/substitution failed:
/mnt/c/Users/Tim/Nextcloud/playground/seqan3/source/hello_world.cpp:250:28: note:   cannot convert ‘rest#0’ (type ‘const char*’) to type ‘int’
  250 |  return add_and_cat(store, rest...);
      |                            ^~~~

非常感谢您的帮助!

【问题讨论】:

标签: c++ function c++11 variadic-templates


【解决方案1】:

int 重载必须调用string 重载时,即当int 参数先于string 参数时,它会失败。您必须先声明该函数,然后才能调用它:

#include <iostream>

//...

// declare "full case for string" called in "full case for int"
template<typename ...Ts>
void add_and_cat(std::pair<int, std::string>& store, std::string s, Ts... rest);

//full case for int
template<typename ...Ts>
void add_and_cat(std::pair<int, std::string>& store, int i, Ts... rest) {
    store.first -= i;
    add_and_cat(store, rest...);
}

//full case for string
template<typename ...Ts>
void add_and_cat(std::pair<int, std::string>& store, std::string s, Ts... rest) {
    store.second += s;
    add_and_cat(store, rest...);
}


int main()
{
    std::pair<int, std::string> p{0,"START"};
    add_and_cat(p, 1, "A", 1, "B"); pp(p);             // no compile error
    add_and_cat(p, 1, 2, "A",3); pp(p);                // no compile error
    add_and_cat(p, "A", 1, 2, "B","C"); pp(p);        // no compile error
    add_and_cat(p, 1, 2, "B","C"); pp(p);             // no compile error
}

Live Demo

请注意,您可以使用 fold expression 避免递归:

void add_one(std::pair<int,std::string>& store,int x){ store.first -=x; }
void add_one(std::pair<int,std::string>& store,const std::string& x){ store.second +=x; }

template <typename ...Ts>
void add_and_cat2(std::pair<int,std::string>& store,Ts... t){
    (add_one(store,t),...);
}

Live Demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    • 2012-03-12
    • 2019-02-04
    • 1970-01-01
    • 2013-04-22
    相关资源
    最近更新 更多