CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Apr 2010
    Posts
    172

    Pair<> with 3 elements?

    is it possible to alter the Pair function to make it use 3 elements?

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Pair<> with 3 elements?

    What pair function are talking about ?

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Pair<> with 3 elements?

    Check out the <tuple> standard library if your compiler is new enough to support it. Otherwise, you can get it from Boost.

  4. #4
    Join Date
    Aug 2009
    Posts
    219

    Re: Pair<> with 3 elements?

    Would this fit?

    Code:
    std::pair<int a , std::pair < int b, int c >>

  5. #5
    Join Date
    Apr 2010
    Posts
    172

    Re: Pair<> with 3 elements?

    Thanks problem solved!(Y)

  6. #6
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Pair<> with 3 elements?

    Quote Originally Posted by NLscotty View Post
    Code:
    std::pair<int a , std::pair < int b, int c >>
    Personally, I've never been a fan of 'pair'. I only ever use it when I have to to interface with certain STL containers.

    By combining them, it gets even worse!
    Code:
    std::pair<int a ,  std::pair< std::pair < int b, int c >, int d>> quad;
    
    quad.second.first.second; // Actually 'c'!
    It doesn't do much for readability or maintainability.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  7. #7
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Pair<> with 3 elements?

    My only gripe with pair is that it has tought me 1-based index (first second), whereas the tuple equivalent is 0-based index. Oh No!

    Code:
    auto my_pair = std::make_pair(1, "Hi!");
    
    my_pair.first = ...
    my_pair.second= ...
    the equivalent tuple:
    Code:
    auto my_tuple = std::make_tuple("hello", "world!");
    
    std::get<0>(my_tuple)= ... ; //equivalent to "first"
    std::get<1>(my_tuple)= ... ; //equivalent to  "second"
    However:
    Code:
    auto my_tuple = std::make_tuple("hello", "world!");
    
    std::get<1>(my_tuple)= ... ; //NOT equivalent "first"
    std::get<2>(my_tuple)= ... ; //Creates a terrible terrible compilation message
    ----
    On a side note, anybody now why this doesn't work?
    Code:
    get<1>(my_tuple);
    //In function 'int main()':
    //error: 'get' was not declared in this scope
    //note: suggested alternative:
    //note:   'std::get'
    Isn't Koenig (Argument-dependent name) lookup was specifically designed to support this use case?
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  8. #8
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Pair<> with 3 elements?

    Quote Originally Posted by monarch_dodra View Post
    On a side note, anybody now why this doesn't work?
    Quote Originally Posted by n3242
    For simple function names, argument dependent lookup (3.4.2) applies even when the function name is not visible within the scope of the call. This is because the call still has the syntactic form of a function call (3.4.1). But when a function template with explicit template arguments is used, the call does not have the correct syntactic form unless there is a function template with that name visible at the point of the call. If no such name is visible, the call is not syntactically well-formed and argument-dependent lookup does not apply. If some such name is visible, argument dependent lookup applies and additional function templates may be found in other namespaces.
    so, ADL may or may not apply in this case, depending on whether there is an accessible function template with the same name in the caller scope. Yes, the "syntactical" excuse is not that convincing though ...

  9. #9
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Pair<> with 3 elements?

    speaking about std:: pair

    in theory, there's nothing wrong with std:: pair, as long as one uses it with consistent semantics, that is to represent a set of two things of which one is intuitively and exhaustively interpreted as first and the other as second.

    the problem is that the STL itself uses pair the wrong way, as the associative containers insert methods. Personally, I would have preferred a dedicated ( but general ) type template, something like an insertion_point<Iterator> exposing an iterator and a boolean value with descriptive names or with a convenient interface ( a la boost:: optional: an operator* exposing the iterator and an explicit operator bool exposing the boolean "already inserted" content ) ...

  10. #10
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Pair<> with 3 elements?

    Just a small niggle, but I would have liked std::map (which has key/value semantics) to have contained something like 'std::keyvalue' with the members 'key' and 'value', rather than using a generic 'pair' for the job, but we're stuck with it now. I like my variables to mean what they say.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  11. #11
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Pair<> with 3 elements?

    Quote Originally Posted by superbonzo;n3242
    For simple function names, argument dependent lookup (3.4.2) applies even when the function name is not visible within the scope of the call. This is because the call still has the syntactic form of a function call (3.4.1). But when a function template with explicit template arguments is used, the call does not have the correct syntactic form unless there is a function template with that name visible at the point of the call. If no such name is visible, the call is not syntactically well-formed and argument-dependent lookup does not apply. If some such name is visible, argument dependent lookup applies and additional function templates may be found in other namespaces.
    I see, but what exactly does "unless there is a function template with that name visible at the point of the call" mean?

    If I get this correctly, it means you can add "using std::get", at which point, ADP would activate? Which would further mean that if I wrote a md::tuple, then it would call my md::set because I declared using std::set?
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  12. #12
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Pair<> with 3 elements?

    Quote Originally Posted by monarch_dodra View Post
    If I get this correctly, it means you can add "using std::get", at which point, ADP would activate?
    yes

    Quote Originally Posted by monarch_dodra View Post
    Which would further mean that if I wrote a md::tuple, then it would call my md::set because I declared using std::set?
    no, I don't think so. It just means that once the compiler gets aware of the fact that a name is a function template name ( and not a syntactical coincidence due to <,> operators ) then usual lookup rules kick in searching for a function template with a matching signature.

    For example

    Code:
    namespace A { struct X{}; template <class T> void f( const T& ); }
    namespace B { template <class T> void f( T& ); }
    
    int main()
    {
        using namespace B;
    
        f<A::X>( A::X() ); // ok, matches A::f via ADL
    }
    Last edited by superbonzo; February 15th, 2012 at 10:10 AM. Reason: wrong example

  13. #13
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Pair<> with 3 elements?

    Quote Originally Posted by superbonzo View Post
    no, I don't think so. It just means that once the compiler gets aware of the fact that a name is a function template name ( and not a syntactical coincidence due to <,> operators ) then usual lookup rules kick in searching for a function template with a matching signature.
    Hum... You say "no", but then you appear to be showing an example where it does work...

    In this example (that also works), I did not specify the all the template parameters. Do you view this as different from what your example was showing?

    Code:
    namespace A
    {
      template <typename T>
      struct A_class
      { };
    
      template<int N, typename T>
      void f(A_class<T>&)
      { }
    }
    
    namespace B
    {
      template <typename T>
      struct B_class { };
    
      template <int N, typename T>
      void f(B_class<T>&)
      { }
    }
    
    int main()
    {
      using A::f;
    
      B::B_class<int> b;
      f<0>(b);
    }
    I find it super awesome that declaring "using A::f" is sufficient to bring into view and resolve a call to B::f. Goodness I love learning.

    EDIT: PS: I tried to rate your post, but it would appear you have already given me such useful advice in the past that I need to spread reputation to others first
    Last edited by monarch_dodra; February 15th, 2012 at 11:35 AM.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  14. #14
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Pair<> with 3 elements?

    Quote Originally Posted by monarch_dodra View Post
    Do you view this as different from what your example was showing?
    no I don't, they do show the same phenomenon. Probably, I misunderstood your last question in post #11 where you said that "if I wrote a md::tuple, then it would call my md::set because I declared using std::set" that, once transposed to your example, I read as if the line "using A::f;" were replaced by "using A::A_class;" that does not work instead.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured