2014-12-23 3 views
4

저는 C++에 익숙하지 않고 Expression Templates를 이해하려고합니다. Wikipedia에 예제 코드가 있습니다. 나는 프로그램이 어떻게 작동 대부분 이해하지만, 나는이 라인 컴파일러에 의해 해석하는 방법을 명확하지 않다 : 아래의 기본 표현 템플릿 클래스에서연산자 오버로딩에서 명확하지 않은 C++ 구문

operator A&()    { return static_cast<  A&>(*this); } 
operator A const&() const { return static_cast<const A&>(*this); } 

. 일반적으로 연산자 오버로딩의 구문은 return_datatype operator+ (args){body} (예 : + 연산자)이지만 오류가 발생하며 함수의 오류 오버라이드는 오류없이 컴파일됩니다. 아무도이 두 줄을 설명 할 수 있습니까? 운영자가 수행하기 전에 A&A const&은 무엇을할까요? 그리고 A& operator()(){}A const& operator()(){}이 작동하지 않는 이유는 무엇입니까?

no matching function for call to 'Vec::Vec(const Expr<Vec>&)' 
    ExprSum(const Expr<A>& a, const Expr<B>& b): _u(a), _v(b) {} 

-Pranav

전체 코드 :

#include <iostream> 
#include <vector> 
#include <cassert> 

using namespace std; 


template <class A> 
class Expr{ 
public: 
    typedef std::vector<double>   container_type; 
    typedef typename container_type::size_type size_type; 
    typedef typename container_type::value_type value_type; 
    typedef typename container_type::reference reference; 

    size_type size() const {return static_cast<A const&>(*this).size(); } 
    value_type operator [] (size_t i) const {return static_cast<A const&> (*this)[i];} 
    operator A&()    { return static_cast<  A&>(*this); } 
    operator A const&() const { return static_cast<const A&>(*this); } 
}; 


class Vec : public Expr<Vec> { 
private: 
    container_type x; 
public: 
    Vec(){} 
    Vec(size_type length) :x(length) {} 
    size_type size() const { return x.size(); } 

    reference operator [] (size_type i){ 
    assert(i < x.size()); 
    return x[i]; 
    } 
    value_type operator [] (size_type i) const { 
    assert(i < x.size()); 
    return x[i]; 
    } 

    template <class A> 
    void operator = (const Expr<A>& ea){ 
    x.resize(ea.size()); 
    for(size_t i = 0; i < x.size(); i++){ 
     x[i] = ea[i]; 
    } 
    } 

}; 


template <class A, class B> 
class ExprSum : public Expr <ExprSum <A,B> >{ 
private: 
    A _u; 
    B _v; 
public: 
    typedef Vec::size_type size_type; 
    typedef Vec::value_type value_type; 

    ExprSum(const Expr<A>& a, const Expr<B>& b): _u(a), _v(b) {} 
    value_type operator [] (size_t i) const { return (_u[i] + _v[i]); } 
    size_type size() const { return _u.size(); } 
}; 


template <class A, class B> 
ExprSum <A,B> const operator + (Expr<A> const& u, Expr<B> const& v){ 
    return ExprSum <A,B> (u,v); 
} 



int main(){ 

    size_t n = 10; 
    Vec x(n); 
    Vec y(n); 
    Vec z; 

    for(size_t i = 0; i < n; i++){ 
    x[i] = i; 
    y[i] = 2*i; 
    } 

    z = x + y; 

    cout << z[7] << endl; 

    cout << "Hello world!" << endl; 
    return 0; 
} 
+0

변환 연산자 또는 캐스트 연산자라고합니다. 고유 한 유형 간 변환을위한 의미를 제공하는 데 사용됩니다. [here] (http://en.cppreference.com/w/cpp/language/cast_operator)를 참조하십시오. – dreamlax

+0

"작동하지 않음"대신, 가지고있는 문제점을 설명하십시오. –

답변

4

이는 conversion operator입니다 그것은 오류가 있습니다. 일반적인 오버로드 된 연산자와 비슷하지만 지정된 반환 유형이없고 연산자 심볼 대신 변환 대상 유형이 있습니다.

+0

감사합니다 @ 세바스찬 운영자. 제공된 링크가 참으로 내 의심을 분명히했습니다. 고마워요 .Pranav – Pranav

관련 문제