使用decltype的功能参数类型 [英] Function parameter type using decltype

查看:147
本文介绍了使用decltype的功能参数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:此问题中提供的示例不是生产代码,完全没有意义.只是用来说明我的问题.

我正在测试decltype的可能性,尤其是当它用于推断函数参数类型并遇到问题时:

I was testing the possibilities of decltype, especially if it is used to deduce function parameter types, and ran into a problem:

假设有两个这样的类结构:

Suppose there were two classes structured like this:

struct ClassInt
{
    // Note: no default ctor
    ClassInt(int value)
        :   m_Value(value)
    {}

    int  m_Value;
};

struct ClassDouble
{
    // Note: no default ctor
    ClassDouble(double value)
        :   m_Value(value)
    {}

    double  m_Value;
};

现在,我编写了一个函数,该函数以某种方式(通过某种方式)通过字符串检索类型参数的实例(应该是上述之一),并将一个给定值分配给其m_Value成员:

Now, I wrote a function that (somehow) retrieves an instance of a the type parameter (which should be one of the above) by string and assigns a given value to its m_Value member:

template< typename Ty >
Ty* get_fake_ptr() { return nullptr; }


// Retrieve pointer to Ty object by name and assign its value member.
// The problem is that we don't actually have an instance of Ty at the point
// where we want to define the type of the parameter "value".
template< typename Ty >
void assign(std::string name, decltype(get_fake_ptr<Ty>()->m_Value) value)
{
    // Somehow get pointer to a Ty object by name
    Ty* obj = ????;

    // Assign
    obj->m_Value = value;
}

现在,参数value的类型取决于type参数,因为所使用的类在成员m_Value的类型上有所不同.如您所见,我通过使用decltype解决了该问题.现在,通常,您可以在参数上使用decltype,如下所示:

Now, the type of the parameter value is dependent on the type parameter, as the used classes differ in the type of the member m_Value. As you can see, I solved it by using decltype. Now, normally, you would use decltype on a parameter, like this:

template<typename Ty>
void assign(Ty& obj, decltype(obj.m_Value) value);

但这显然是不可能的,因为实际实例是在函数主体中检索的,因此在声明函数自变量的位置不可用.

But that is obviously not possible here since the actual instance is retrieved in the function body and is thus not available at the point where function arguments are declared.

我使用模板函数get_fake_ptr破解了该函数,该函数仅返回匹配类型的nullptr,因此我有一个伪实例",编译器可以使用它来确定成员类型.而且有效:

I hacked it together by using the template function get_fake_ptr which just returns a nullptr of matching type so I have a "pseudo instance" that the compiler can use to determine the member type. And it works:

现在,就像我说的那样,这对我来说似乎真的很不可靠.所以:

Now, as I said, this seems really hacky to me. So:

是否有更好的方法来解决此问题?

谢谢!

推荐答案

您可以使用decltype(std::declval<T>().m_Value)来推断Tm_Value成员的类型,而不是使用get_fake_ptr()函数.他们最终实现了相似的目标.

You can use decltype(std::declval<T>().m_Value) to deduce the type of T's m_Value member instead of using your get_fake_ptr() function. They accomplish similar goals in the end.

顺便说一句,不需要定义您的get_fake_ptr()函数.您可以简单地将其保留为template <typename T> T* get_fake_ptr();,并且仍可以在decltype中使用它.

By the way, your get_fake_ptr() function does not need to be defined. You can simple leave it as template <typename T> T* get_fake_ptr(); and you can still use it inside decltype.

这篇关于使用decltype的功能参数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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