简体   繁体   English

模板类型的隐式转换运算符不会自动确定

[英]Implicit conversion operator for templated types not automatically determined

I have a templated class thing with an implicit conversion operator, like follows: 我有一个模板类thing有一个隐含的转换操作符,就像如下:

#include <stdio.h>

template <typename T>
struct thing
{
    T t;

    operator const T&() const 
    { 
        return t;
    }
};

template <typename T>
struct B 
{
    T t;
};

void fun(const int&) {
    printf("int\n");
}

template <typename T>
void fun(const B<T>&) {
    printf("B<T>\n");
}

int main()
{
    thing<int> a;
    fun(a);

    thing<B<int>> b;
    fun(b);

    return 0;
}

Calling fun(const int&) with a thing<int> , the compiler is able to figure out to invoke the implicit conversion operator in order to pass the const T& (in this case const int& ) to fun(const int&) . 使用thing<int>调用fun(const int&) ,编译器能够找出调用隐式转换运算符,以便将const T& (在本例中为const int& )传递给fun(const int&)

However, for a thing<B<int>> , the compiler can not figure out that I expect fun(const B<T>&) to be invoked. 但是,对于一个thing<B<int>>thing<B<int>> ,编译器无法弄清楚我希望调用fun(const B<T>&)

How can I help the compiler figuring this out without casting b to const B<int>& explicitely (using static_cast<const B<int>&>(b) for instance)? 如何帮助编译器解决这个问题而不bconst B<int>& explicitely(例如使用static_cast<const B<int>&>(b) )?

My concrete usage scenario is similar to the code provided with the constraints that I am using B with ~10 different types T , ie not arbitrary many different T s. 我的具体使用场景类似于我使用B约束的代码,其中约有10种不同类型的T ,即不是任意多个不同的T s。 If I have to create ~10 template specializations, so be it. 如果我必须创建~10个模板专业化,那就这样吧。 However, I don't exactly know how to best overload struct B in that case. 但是,在这种情况下,我并不完全知道如何最好地重载struct B But maybe I am on the wrong track - do simpler/more elegant solutions exist, maybe? 但也许我走错了路 - 可能存在更简单/更优雅的解决方案吗?

How can I help the compiler figuring this out without casting b to const B<int>& ? 如何在不将bconst B<int>&情况下帮助编译器解决这个问题?

You can't. 你不能。 Templates do not do any implicit conversion. 模板不进行任何隐式转换。 They deduce the type of the parameter and that is the type they use. 他们推断出参数的类型,即他们使用的类型。

One thing you can do is add a get function to your wrapper like 你可以做的一件事就是给你的包装器添加一个get函数

template <typename T>
struct thing
{
    T t;

    operator const T&() const 
    { 
        return t;
    }
    const T& get() const 
    { 
        return t; 
    }
};

and then you can call fun like 然后你可以称之为fun

fun(b.get());

Template argument deduction doesn't consider implicit conversion. 模板参数推导不考虑隐式转换。

Type deduction does not consider implicit conversions (other than type adjustments listed above): that's the job for overload resolution , which happens later. 类型推导不考虑隐式转换(上面列出的类型调整除外):这是重载解析的工作,稍后会发生。

Then given fun(b); 然后给予fun(b); , the template fun can't be invoked because T can't be deduced. ,模板fun无法调用,因为无法推断出T

You can specify the template argument explicitly, then overload resolution and implicit conversion would work fine. 您可以显式指定模板参数,然后重载解析和隐式转换可以正常工作。

fun<int>(b);

LIVE 生活

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM