2016-05-16 19 views
7

Vì vậy, Nếu tôi có một cái gì đó như thế nàyđầu ra quá tải << nhà điều hành cho một lớp học để in các tuple bên trong nó

template<typename... Args > 
class tuple_class 
{ 
    public: 
    std::tuple<Args...> tup; 

/*I left out the other functions */ 
}; 

Tôi muốn nạp chồng toán tử < < để nó sẽ đệ quy in tuple khi kêu gọi lớp.

ví dụ:

auto a = tuple_class(1, 2 ,3); 
std::cout << a << endl; 

hy vọng sẽ in '123'

Ive nhìn thấy ví dụ khác về máy in tuple nhưng tôi không thể áp dụng nó vào lớp học của tôi mà không cần phải một loạt các rắc rối

Tôi nghĩ rằng tôi nên bắt đầu với một hàm thành viên như thế này

template<typename... Args> 
    friend std::ostream& operator<<(std::ostream& os, const my_tuple<Args...> &m); 

và sau đó là chức năng thực tế bên ngoài lớp

template<typename... Args> 
std::ostream& operator<<(std::ostream& os, const my_tuple<Args...> &m) 
{ 
    os << "SOMETHING" << std::endl; 
    return os; 
} 

Điều đó thực sự hiệu quả khi tôi gọi nhà điều hành < < trên lớp của tôi. Nhưng tôi không có đầu mối làm thế nào để làm cho nó thực sự in tuple.

Bất kỳ trợ giúp sẽ được đánh giá

+1

Bạn cần phải đệ quy đệ quy mẫu variadic thông qua chuyên môn từng phần. Xem [câu hỏi này] (http://stackoverflow.com/questions/7124969/recursive-variadic-template-to-print-out-the-contents-of-a-parameter-pack). – denniskb

Trả lời

2

Xây dựng giải pháp tôi đã sử dụng mã in tuple từ cppreference đề cập ở đây this. Phần còn lại của mã là keo để giữ mọi thứ lại với nhau. Here Tôi đặt mẫu làm việc.

#include <tuple> 
#include <iostream> 
#include <string> 

// tuple printer 
template<class Tuple, std::size_t N> 
struct TuplePrinter { 
    static std::ostream& print(std::ostream& os, const Tuple& t) 
    { 
     TuplePrinter<Tuple, N - 1>::print(os, t); 
     os << ", " << std::get<N - 1>(t); 
     return os; 
    } 
}; 

template<class Tuple> 
struct TuplePrinter<Tuple, 1> { 
    static std::ostream& print(std::ostream& os, const Tuple& t) 
    { 
     os << std::get<0>(t); 
     return os; 
    } 
}; 

template<class... Args> 
std::ostream& print(std::ostream& os, const std::tuple<Args...>& t) 
{ 
    os << "("; 
    TuplePrinter<decltype(t), sizeof...(Args)>::print(os, t); 
    os << ")\n"; 
    return os; 
} 


// class to keep tuple inside 
template<typename... Args> 
class tuple_class { 
    template<typename... Args2> 
    friend std::ostream& operator<<(std::ostream& os, const tuple_class<Args2...> &m); 

    std::tuple<Args...> tup; 
public: 
    tuple_class(Args&&... args) : tup(std::forward<Args>(args)...) { 

    } 
}; 

// usage of the printer 
template<typename... Args> 
std::ostream& operator<<(std::ostream& os, const tuple_class<Args...> &m) { 
    print(os, m.tup); 
    return os; 
} 



int main() { 
    tuple_class<int,float,std::string> tc(1,3.0f,"string"); 
    std::cout << tc; 
    return 0; 
} 
+0

Cảm ơn bạn đã phản hồi, khi tôi thử điều này, tôi nhận được 2 lỗi: lỗi: không thể liên kết ‘std :: ostream {aka std :: basic_ostream }’ lvalue thành ‘std :: basic_ostream &&’ os << std::get<0> (t); và lỗi: không khớp với 'toán tử <<' (các kiểu toán hạng là 'std :: ostream {aka std :: basic_ostream }' và 'std :: __ tuple_element_t <0ul, – user2770808

+0

Bạn có thể cung cấp cho tôi phiên bản trình biên dịch chính xác của bạn hay không biên dịch cờ bạn, được sử dụng? – tomekpe

+0

Tôi cố định nó đã làm với cách tôi đã xây dựng tuple của tôi. – user2770808

0

Nếu bạn có thể làm mà không có bộ dữ liệu chuẩn, tôi đề xuất giải pháp đơn giản sau đây (Tôi hy vọng như vậy).

#include <iostream> 

template <typename ... Args > 
class my_tuple; 

template <typename A0, typename ... Args > 
class my_tuple<A0, Args...> 
{ 
    private: 

     A0     elem; 
     my_tuple<Args...> next; 

    public: 

     my_tuple (const A0 & a0, const Args & ... args) 
     : elem { a0 }, next { args ... } 
     { } 

     /*I left out the other functions */ 

     friend std::ostream& operator<< (std::ostream & os, 
             const my_tuple<A0, Args...> & m) 
     { return os << m.elem << m.next; } 
}; 

template <> 
class my_tuple<> 
{ 
    public: 

     friend std::ostream& operator<< (std::ostream & os, 
             const my_tuple<> &) 
     { return os; } 
}; 

int main() 
{ 
    my_tuple<int, float, long> mt1 { 12, 23.4, 45L }; 
    my_tuple<int, int, int>  mt2 { 1, 2, 3 }; 

    std::cout << "my tuple 1 [" << mt1 << ']' << std::endl; 
    std::cout << "my tuple 2 [" << mt2 << ']' << std::endl; 

    return 0; 
} 
Các vấn đề liên quan