从左值扣除模板返回类型? [英] Template return type deduction from lvalue?

查看:48
本文介绍了从左值扣除模板返回类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但是我还是想澄清一下.可以说我有一个像这样的模板函数:

This might be a silly question but I would like to have it clarified none the less. Lets say I have a template function like so:

template<class T> T getValue(const char *key) const;

从内部存储中以 T 形式返回值,该值存储在 key 下(可能已经是T类型).

that returns the value as T from internal storage where it is stored under key (and possibly as type T already).

现在,要使用此功能,我需要在函数调用中指定模板返回类型 T ,例如:

Now in order to use this I need to specify the template return type T in the function call, for example:

int value = getValue<int>("myKey");

我想做的是从上下文中推断出模板参数,特别是 lvalue ,像这样:

while what I would want it to do is deduce the template argument from the context, specifically the lvalue like so:

int value = getValue("myKey"); //getValue<int>() is instantiated with int being deduced automatically from lvalue

但是我猜测这是不可能的,但是对于原因我还是很模糊.我知道使用 auto 将使编译器无法推断出模板类型,但是为什么这样呢?

but I am guessing that this is not possible but I am rather fuzzy as to why. I know using auto would make it impossible for the compiler to deduce the template type but why this is as well?

推荐答案

模板实例化只能从参数推断给定模板对象的参数(在这种情况下为函数),所以不,变量类型在推论上没有关系,并且您要么必须向函数提供类型T的伪参数,要么像在倒数第二个脚本代码( getValue< int>(...))中那样对它进行硬编码.

Template instantiation can only deduce its parameters from the arguments to given templated object(function in this case) so no, the variable type does not matter in deducing, and you either have to provide dummy argument of type T to the function or hardcode it as you did in the second to last script code(getValue<int>(...)).

在注释中提供了使用类型推导的一种可能的解决方法:

There is a possible workaround using type deduction presented in the comments :

#include <iostream>

namespace byte_read {
    //this is a hack to deduce the type using implicit conversion
    struct type_converter {
        const char* buffer;

        template<typename T>
            operator T() {
            std::cout << "implicit convertion from " << typeid(buffer).name() 
                << " to " << typeid(T).name() << std::endl;
            //casting memory to the desired type
            return static_cast<T>(*buffer);
        }
    };
    type_converter getValue(const char * buffer) {
        //here buffer is implicitly converted to T type using the operator T()
        return {buffer};
    }

}
using namespace byte_read;

int main()
{
    char buffer[]{0,1,0,0 //int 256 encoded
                  ,97      //char 'a' encoded
                 };
    //pointer to read the buffer sequentialy
    char* pos = buffer;
    //pointer used to count the bytes readed
    char* last_pos = pos;

    int int_256 = getValue(pos);
    pos+=sizeof(int);
    std::cout << int_256 << " bytes readed :" << pos - last_pos << std::endl;

    last_pos = pos;
    char char_a = getValue(pos);
    pos+=sizeof(char);
    std::cout << char_a << " bytes readed :" << pos - last_pos << std::endl;

}

您可以在此处

这篇关于从左值扣除模板返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆