简体   繁体   English

双模板化函数重载失败

[英]Double templated function overload fails

I have a template class, having various template functions. 我有一个模板类,具有各种模板功能。 One of them need to be overloaded (a couple of times). 其中之一需要过载(几次)。

Basically - if my class would not be a template, these would be my function(s): 基本上- 如果我的课程不是模板,那么这些将是我的函数:

class OtherClass
{
public:
    template<class T> T foo(T &t, const std::string &trash);
};

template<class T>
T OtherClass::foo(T &t, const std::string &trash)
{
    return t; //...
}

template<>
std::wstring OtherClass::foo<std::wstring>(std::wstring &w, const std::string &trash)
{
    return w; //...
}

This: 这个:

int main(...)
{
    int i = 0;
    std::wstring w;

    OtherClass o;
    i = o.foo(i, std::string(""));
    w = o.foo(w, std::string(""));
}

My template class looks like: 我的模板类如下:

template<class MStr>
class OtherClass
{
public:
    template<class TVal> TVal foo(TVal &t, const MStr &trash);
};

//Which leads to the function definition

template<class MStr>
template<class TVal>
TVal OtherClass<MStr>::foo(TVal &t, const MStr &trash)
{
    return t; //...
}

What I wanted to have... (int as example) 我想拥有的...(以int为例)

template<class MStr>
template<>
int OtherClass<MStr>::foo<int>(int &t, const MStr &trash)
{
    return t; //...
}

Welcome in the land of 欢迎来到
C2768: illegal use of explicit template arguments
and
C2244: unable to match function definition

1>...\test\main.cpp(74): error C2768: 'OtherClass<MStr>::foo' : illegal use of explicit template arguments
1>...\test\main.cpp(74): error C2768: 'OtherClass<MStr>::foo' : illegal use of explicit template arguments
1>...\test\main.cpp(74): error C2244: 'OtherClass<MStr>::foo' : unable to match function definition to an existing declaration
1>          definition
1>          'int OtherClass<MStr>::foo<int>(int &,const MStr &)'
1>          existing declarations
1>          'TVal OtherClass<MStr>::foo(TVal &,const MStr &)'
1>
1>Build FAILED.

I have been testing, and looking for hours in Google and Stackoverflow... so far the best answer/question, which does not seem to be applicable for me was this. 我一直在测试,并在Google和Stackoverflow中找了几个小时...到目前为止,最好的答案/问题似乎并不适合我

Q: Is there anyone who could point me in the right direction, or has a fix for it, in order to solve this issue ? 问:是否有人可以为我指出正确的方向,或者有针对性的解决方案来解决此问题?

A way to sidestep this issue is to just declare that int version as an overload rather than an template specialization: 避免此问题的一种方法是,将int版本声明为重载而不是模板专门化:

template<class MStr>
class OtherClass
{
public:
    template<class TVal> TVal foo(TVal &t, const MStr &trash);
    int foo(int &t, const MStr &trash);
};

Then defined as: 然后定义为:

template<class MStr>
int OtherClass<MStr>::foo(int &t, const MStr &trash)
{
    return t; //...
}

It's not particularly pretty if you have many overload cases, but it probably beats any other solutions. 如果有很多重载情况,它不是特别漂亮,但是它可能胜过其他任何解决方案。

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

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