代码之家  ›  专栏  ›  技术社区  ›  Alexandre C.

std::引用对

  •  32
  • Alexandre C.  · 技术社区  · 15 年前

    有一张支票有效吗 std::pair this link ,似乎没有对operator=进行特殊处理,因此无法生成默认赋值运算符。

    我想喝一杯 pair<T&, U&> 并且能够为它分配另一对(值或引用),并修改指向的对象。

    7 回复  |  直到 7 年前
        1
  •  30
  •   Community CDub    8 年前

    不,是你 在C++ 03中可靠地执行此操作,因为 pair 引用 T reference to a reference is not legal 在C++ 03中。

    注意我说的“可靠”。一些常用的编译器还在使用中(对于GCC,我测试了GCC4.1, @Charles 报告的GCC4.4.4)不允许形成引用,但最近 当它们实现引用折叠时允许它( T& T型 T型 是引用类型)。如果您的代码使用了这样的东西,您就不能依赖它在其他编译器上工作,除非您尝试并看到它。

    boost::tuple<>

    int a, b;
    
    // on the fly
    boost::tie(a, b) = std::make_pair(1, 2);
    
    // as variable
    boost::tuple<int&, int&> t = boost::tie(a, b);
    t.get<0>() = 1;
    t.get<1>() = 2;
    
        2
  •  35
  •   user283145 user283145    12 年前

    std::pair< std::reference_wrapper <T>, std::reference_wrapper<U>> 这种类型的对象的行为将完全符合您的要求。

        3
  •  8
  •   sbi    15 年前

    我认为拥有一个 std::pair std::map 使用 标准::对 用一个 const 毕竟,这两种类型都不能赋值。

    pair<T&, U&> 并能给它分配另一对

        4
  •  2
  •   Yakov Galka    15 年前

    你是对的。可以创建一对引用,但不能使用 operator = 不再。

        5
  •  2
  •   MrMasterplan    14 年前

    template <class T1, class T2> struct refpair{
        T1& first;
        T2& second;
        refpair(T1& x, T2& y) : first(x), second(y) {}
        template <class U, class V>
            refpair<T1,T2>& operator=(const std::pair<U,V> &p){
                first=p.first;
                second=p.second;
                return *this;
            }
    };
    

    它允许你做一些可怕的事情,比如:

    int main (){
    
        int k,v;
        refpair<int,int> p(k,v);
    
        std::map<int,int>m;
        m[20]=100;
        m[40]=1000;
        m[60]=3;
    
        BOOST_FOREACH(p,m){
            std::cout << "k, v = " << k << ", " << v << std::endl;      
        }
        return 0;
    }
    

    k v 我分配给的都藏在里面了 p . 如果你这样做,它几乎又变漂亮了:

    template <class T1,class T2>
    refpair<T1,T2> make_refpair (T1& x, T2& y){
        return ( refpair<T1,T2>(x,y) );
    }
    

    它允许你这样循环:

    BOOST_FOREACH(make_refpair(k,v),m){
        std::cout << "k, v = " << k << ", " << v << std::endl;      
    }
    

    (欢迎所有评论,因为我绝不是c++专家。)

        6
  •  1
  •   alfC    12 年前

    我不知道这是怎么回事 std::pair 在C++ 03中,但是如果我天真地重新实现它,我就不会有任何问题了(使用相同的编译器) gcc clang

    double a = 1.;
    double b = 2.;
    my::pair<double, double> p1(5., 6.);
    my::pair<double&, double&> p2(a, b);
    p2 = p1; // a == 5.
    

    所以一个解决方法是(1)重新实现 pair (在另一个命名空间中)或(2)专门化 std::pair<T&, T&> 或者(3)简单地使用C++ 11(在哪里) 参考文献(开箱即用)

    (1) 这里是幼稚的实现

    namespace my{
    template<class T1, class T2>
    struct pair{
        typedef T1 first_type;
        typedef T2 second_type;
        T1 first;
        T2 second;
        pair(T1 const& t1, T2 const& t2) : first(t1), second(t2){}
        template<class U1, class U2> pair(pair<U1, U2> const& p) : first(p.first), second(p.second){}
        template<class U1, class U2> 
        pair& operator=(const pair<U1, U2>& p){
          first = p.first;
          second = p.second;
          return *this;
        }
    };
    template<class T1, class T2>
    pair<T1, T2> make_pair(T1 t1, T2 t2){
        return pair<T1, T2>(t1, t2);
    }
    }
    

    (2) 在这里它是 (有些人可能会抱怨我在忙着超载/专攻机器。) std

    namespace std{
        template<class T1, class T2>
        struct pair<T1&, T2&>{
            typedef T1& first_type;    /// @c first_type is the first bound type
            typedef T2& second_type;   /// @c second_type is the second bound type
            first_type first;
            second_type second;
            pair(T1& t1, T2& t2) : first(t1), second(t2){}
            template<class U1, class U2> pair(pair<U1, U2> const& p) : first(p.first), second(p.second){}
            template<class U1, class U2> 
            pair& operator=(const pair<U1, U2>& p){
              first = p.first;
              second = p.second;
              return *this;
            }
        };
    }
    

    也许我遗漏了一些明显的问题,如果有明显的缺陷,我可以修改答案。

        7
  •  -1
  •   Zachary Kraus    11 年前

    我最终解决了一个类似的问题,只是建立了一个非常简单的结构。我甚至不担心赋值操作符,因为默认操作符应该可以正常工作。

    template<class U, class V>
    struct pair
    {
    pair(U & first, V & second): first(first), second(second) {}
    U & first;
    V & second;
    }