-
February 14th, 2012, 12:11 PM
#1
Pair<> with 3 elements?
is it possible to alter the Pair function to make it use 3 elements?
-
February 14th, 2012, 12:18 PM
#2
Re: Pair<> with 3 elements?
What pair function are talking about ?
-
February 14th, 2012, 12:21 PM
#3
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.
-
February 14th, 2012, 12:41 PM
#4
Re: Pair<> with 3 elements?
Would this fit?
Code:
std::pair<int a , std::pair < int b, int c >>
-
February 14th, 2012, 12:49 PM
#5
Re: Pair<> with 3 elements?
Thanks problem solved!(Y)
-
February 15th, 2012, 04:05 AM
#6
Re: Pair<> with 3 elements?
Originally Posted by NLscotty
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
-
February 15th, 2012, 04:41 AM
#7
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.
-
February 15th, 2012, 05:04 AM
#8
Re: Pair<> with 3 elements?
Originally Posted by monarch_dodra
On a side note, anybody now why this doesn't work?
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 ...
-
February 15th, 2012, 05:17 AM
#9
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 ) ...
-
February 15th, 2012, 05:38 AM
#10
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
-
February 15th, 2012, 07:47 AM
#11
Re: Pair<> with 3 elements?
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.
-
February 15th, 2012, 09:49 AM
#12
Re: Pair<> with 3 elements?
Originally Posted by monarch_dodra
If I get this correctly, it means you can add "using std::get", at which point, ADP would activate?
yes
Originally Posted by monarch_dodra
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
-
February 15th, 2012, 11:30 AM
#13
Re: Pair<> with 3 elements?
Originally Posted by superbonzo
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.
-
February 15th, 2012, 12:21 PM
#14
Re: Pair<> with 3 elements?
Originally Posted by monarch_dodra
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|