简体   繁体   English

表达式模板 - 无法专门化功能模板

[英]Expression Template - Failed to specialize function template

I was attempting to make some expression templates as an answer to this question , but I'm getting compiler errors, that I can't figure out. 我试图制作一些表达模板作为这个问题的答案,但我收到了编译错误,我无法弄明白。 I have gotten the SSCCE quite small by now 我现在已经把SSCCE变得很小了

template<class sub_expr>
class inherit2 : private sub_expr { //line 3
public:
    inherit2(sub_expr rhs) : sub_expr(rhs) {}
    template<class T>
    auto operator()(const T& v) const ->decltype(sub_expr::operator()(v)) //line 7
    {return sub_expr::operator()(v);}
};

class expression_parameter {
public:
    template<class T>
    const T& operator()(const T& v) const {return v;}
};

int main() {
    expression_parameter x;
    auto expr0 = x;
    int res0 = expr0(3); //line 20
    auto expr1 = inherit2<expression_parameter>(x); //line 21
    int res1 = expr1(3); //line 22
    return 0;
}

When I compile with MSVC10++ I get this error: 当我使用MSVC10 ++编译时,我收到此错误:

f:\code\utilities\exprtemplate\exprtemplate\sscce.cpp(22): error C2893: Failed to specialize function template ''unknown-type' inherit2<sub_expr>::operator ()(const T &) const'
with
[
    sub_expr=expression_parameter
]
With the following template arguments:
'int'

When I compile with GCC 4.6.3: 当我使用GCC 4.6.3编译时:

sscce.cpp: In instantiation of 'inherit2<expression_parameter>':
sscce.cpp:21:47:   instantiated from here
sscce.cpp:3:7: warning: base class 'class expression_parameter' has a non-virtual destructor [-Weffc++]
sscce.cpp: In function 'int main()':
sscce.cpp:22:20: error: no match for call to '(inherit2<expression_parameter>) (int)'
sscce.cpp:3:7: note: candidate is:
sscce.cpp:7:10: note: template<class T> decltype (sub_expr:: operator()(v)) inherit2::operator()(const T&) const [with T = T, sub_expr = expression_parameter, decltype (sub_expr:: operator()(v)) = decltype (expression_parameter::operator()(v))]
sscce.cpp:20:6: warning: unused variable 'res0' [-Wunused-variable]
sscce.cpp:22:6: warning: unused variable 'res1' [-Wunused-variable]

And finally Clang 3.1 最后是Clang 3.1

sscce.cpp(22,12) :  error: no matching function for call to object of type 'inherit2<expression_parameter>'
        int res1 = expr1(3);
                   ^~~~~
sscce.cpp(7,9) :  note: candidate template ignored: substitution failure [with T = int]
    auto operator()(const T& v) const ->decltype(sub_expr::operator()(v))
         ^

So in summary: it appears that I got the decltype wrong, but I can't figure out the correct way. 总而言之: 看来我的decltype错了,但我无法弄清楚正确的方法。 Can anyone help me figure out what is causing these errors? 任何人都可以帮我找出造成这些错误的原因吗?

Some of the errors I got from GCC suggested to me that there was an ambiguous base involved. 我从GCC得到的一些错误告诉我,涉及到一个模糊的基础。 Namely, expression_multiply<expression_parameter, expression_parameter> has expression_parameter as a (direct) base, and inherit_again<expression_parameter> which itself has expression_parameter as a base. 即, expression_multiply<expression_parameter, expression_parameter>具有expression_parameter作为(直接)基础,以及inherit_again<expression_parameter> ,其本身具有expression_parameter作为基础。 This means that lhs::operator() is ambiguous in the scope of expression_multiply<expression_parameter, expression_parameter> (where lhs is in fact expression_parameter . 这意味着lhs::operator()expression_multiply<expression_parameter, expression_parameter> (其中lhs实际上是expression_parameter lhs::operator()的范围内是不明确的。

A further fix was needed in that this was needed in the return type of operator() , resulting in the following fixes: 需要进一步的修复在this需要在的返回类型operator()导致以下修正:

// Instead of inherit_again
template<int N, class sub_expr>
class base : public sub_expr {
public:
    base(sub_expr rhs): sub_expr(std::move(rhs)) {}
};

template<class lhs_given, class rhs_given>
class expression_multiply: private base<0, lhs_given>, private base<1, rhs_given> {
    typedef base<0, lhs_given> lhs;
    typedef base<1, rhs_given> rhs;
public:
    expression_multiply(lhs_given l, rhs_given r):lhs(std::move(l)), rhs(std::move(r)) {}
    template<class T>
    auto operator()(const T& v) const
    -> decltype(this->lhs::operator()(v) * this->rhs::operator()(v))
    {
        return lhs::operator()(v) * rhs::operator()(v);
    }
};

No idea if the compiler is behaving correcting regarding the need for those two 'fixes'. 不知道编译器是否正在对这两个“修复”的需求进行纠正。

This is an addendum to Luc's fix. 这是Luc的解决方案的附录。 Use: 使用:

decltype(std::declval<lhs>()( v ) * std::declval<rhs>()( v ) )

To help the compiler deduce the type without much ado. 为了帮助编译器在没有太多麻烦的情况下推断出类型。

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

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