c++ - 使用模板化方法处理意外行为

标签 c++ templates variadic-templates implicit-conversion stdtuple

我正在研究一个应该(自其 HW 起)由可变参数模板解决的问题。我缺乏理解使我无法解决以下错误。

代码是:

#include <sstream>
#include <iostream>
#include <string>
#include <vector>
#include <tuple>

template<typename ... TL>
class LazySplitResult
{
public:
    LazySplitResult(TL ...pl) :storedParams(pl ...) {       }

    void doStuff()
    {
        // invoke method with inside parameters
        useStoredTuple(storedParams, std::index_sequence_for<TL...>());
    }
    template<typename T, typename T1, typename... Targs>
    void doInsideLogic(T&& value1, Targs&&  ... Fargs)
    {
        // there is some logic
        // for example this
        std::stringstream convert("0");
        // it works for string,double,int ... other types are not supported
        // so exception would be in place
        convert >> value1;
    }

    void doInsideLogic()
    {
    }       

private:
    template<std::size_t... Is>
    void useStoredTuple(const std::tuple<TL ...>& tuple,std::index_sequence<Is...>) {
        wrapInsideLogic(std::get<Is>(tuple) ...);
    }

    void  wrapInsideLogic(TL && ... args)
    {
        doInsideLogic(args...);
    }

    std::tuple<TL ...> storedParams;
};

template<typename ...TL>
LazySplitResult<TL ...> getResult(TL && ...pl)
{
    return LazySplitResult<TL...>(pl  ...);
}

int main()
{
    std::string x;
    int y;
    double z;

    // prepares an object
    auto lazyResult=getResult(x, '.', '-', y, 'x', z , 'a');

    // let it do its thing and set unset variables
    lazyResult.doStuff();

    std::cout << "x = " << x << ", y = " << y << ", z = " << z << std::endl;

    return 0;
}

错误信息在这里

error C2664: 'void LazySplitResult<std::string &,char,char,int &,char,double &,char>::wrapInsideLogic(std::string &,char &&,char &&,int &,char &&,double &,char &&)': cannot convert argument 2 from 'const char' to 'char &&'
source_file.cpp(40): note: Conversion loses qualifiers
source_file.cpp(18): note: see reference to function template instantiation 'void LazySplitResult<std::string &,char,char,int &,char,double &,char>::useStoredTuple<0,1,2,3,4,5,6>(const std::tuple<std::string &,char,char,int &,char,double &,char> &,std::integer_sequence<_Ty,0,1,2,3,4,5,6>)'

复制自rextester here .

HW 的主要部分是解析公式并将结果保存在变量中,如下所示:

// declare variables and initialize class with formula

parse(x, '.', '-', y, 'x', z , 'a');   
std::cout << "x = " << x << ", y = " << y << ", z = " << z << std::endl;

代码做同样的事情,除了它以惰性方式执行,因此需要将参数存储在元组中以备后用。

我尝试在不使用元组和类的情况下成功实现相同的逻辑,除了懒惰。可以观察here .没有出现错误描述转换错误。

你能帮帮我吗?我完全迷路了。入口方法调用是相同的,处理参数的可变参数模板方法具有相同的参数......唯一的区别似乎是中间的元组和类。

最佳答案

首先,有一个无用的模板参数T1在你的doInsideLogic函数模板。将其删除。

wrapInsideLogic的参数自 TL 以来不是转发引用是已知的而不是推导出来的。它的论点std::get<Is>(tuple)类型为 const TL&tuple类型为 const std::tuple<TL...>& , 因此不匹配参数类型 TL&& .要解决此问题,有两种选择:

  1. 使用转发引用,即

    template <typename... Targs>
    void wrapInsideLogic(Targs&&... args)
    {
        doInsideLogic(std::forward<Targs>(args)...);
    } 
    
  2. 使用对常量的引用,即

    void wrapInsideLogic(const TL&... args)
    {
        doInsideLogic(args...);
    } 
    

关于c++ - 使用模板化方法处理意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49702431/

相关文章:

asp.net - 多项目模板文件夹结构不正确

c++ - 编译时间间隔检查器

c++11 - 可变参数模板继承中的运算符重载

C++ 模板部分特化 : Why cant I match the last type in variadic-template?

c++ - 点燃 SqlFieldsQuery 特定键

c++ - 引用Abaqus C++ API静态库读取ODB文件

C++、count_if 和 equals

c++ - 处理特定类型元素的任何容器的非模板函数

c++ - 在可变参数模板中传递参数组

java - 我对 java 和 c++ 之间的 "reference"感到困惑