使用模板化方法的 char 意外行为

char unexpected behaviour with templated methods

本文关键字:char 意外 方法      更新时间:2023-10-16

我正在研究一个应该(因为它的硬件(应该通过可变参数模板解决的问题。我缺乏理解使我无法解决以下错误。

代码为:

#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复制的。

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

// declare variables and initialize class with formula
parse(x, '.', '-', y, 'x', z , 'a');   
std::cout << "x = " << x << ", y = " << y << ", z = " << z << std::endl;
代码执行相同的操作,

只是它以惰性方式执行,因此参数需要存储在元组中以供以后使用。

我试图实现相同的逻辑,除了懒惰,没有成功使用元组和类。可以在这里观察到。未引发描述转换错误的错误。

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

首先,doInsideLogic函数模板中T1了一个无用的模板参数。删除它。

wrapInsideLogic 的参数不是转发参考,因为TL是已知的,不会被推导。它的参数std::get<Is>(tuple)const TL&类型,因为tupleconst 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...);
    } 
    

不确定...您是向第一个链接引用的代码寻求帮助,还是为第二个链接引用的代码寻求帮助?

我想是第二个代码,所以我建议按如下方式重新排序函数的定义。

首先是递归的基本情况

void myFunc ()
 { std::cout << "end" << std::endl; }

二、递归版myFunc()

template <typename T, typename ... Targs>
void myFunc (T && value1, Targs && ... Fargs)
 {
   // there should be additional logic 
   // if value1 is char then skip it. But it was intentionally ommited
   std::cout << " do" << std::endl;
   const char* parsedValue = "0";
   std::stringstream convert(parsedValue);
   // cast current value according to thing.
   convert >> value1;
   myFunc(Fargs ...);
 }

三、test()

template <typename ... Targs>
void test (Targs && ... args)
 { myFunc(args...); }

现在你的代码应该编译了。

如果你先写test(),那么递归myFunc(),最后一个基本情况myFunc()你有 (1( test()调用myFunc()当未声明和 (2( 递归myFunc()调用基本情况myFunc()何时未声明。