[MPL] unique not working?
I pass a mpl::vector with duplicates to mpl::unique and the result still
has duplicates.
What am I doing wrong?
http://coliru.stacked-crooked.com/a/fe85b5739eb4c15f
#include
Nevermind I think I understand, it works just like std::unique in that it works on consecutive groups.
On Oct 13, 2013, at 11:44 PM, Michael Marcin
wrote: I pass a mpl::vector with duplicates to mpl::unique and the result still has duplicates. What am I doing wrong?
Just like std::unique, the duplicated elements must be consecutive for mpl::unique to find them. Otherwise the complexity wouldn't be linear.
http://coliru.stacked-crooked.com/a/fe85b5739eb4c15f
#include
#include #include #include #include #include template< typename... Args > void test( const Args&... args ) { using namespace boost::mpl; using namespace boost::mpl::placeholders; typedef vector
args_types; typedef typename unique >::type unique_types; print< unique_types > x; } int main() { int a = 0; char b = 0; test( a, b, a ); }
+ g++-4.8 -std=c++11 -O2 -Wall -pedantic -pthread main.cpp main.cpp: In instantiation of ‘void test(const Args& ...) [with Args = {int, char, int}]’: main.cpp:22:19: required from here main.cpp:15:27: warning: unused variable ‘x’ [-Wunused-variable] print< unique_types > x; ^ + ./a.out
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Mon, Oct 14, 2013 at 12:56 AM, Gordon Woodhull
On Oct 13, 2013, at 11:44 PM, Michael Marcin
wrote: I pass a mpl::vector with duplicates to mpl::unique and the result still has duplicates. What am I doing wrong?
Just like std::unique, the duplicated elements must be consecutive for mpl::unique to find them. Otherwise the complexity wouldn't be linear.
Michael, Use mpl::sort ( http://www.boost.org/doc/libs/1_54_0/libs/mpl/doc/refmanual/sort.html) first on your sequence. Rodrigo
Then again, taking a deeper look on your snippet, you are sorting all kinds of types. I remember seeing something similar in a thread here and found it at [1]. It works by creating an ascending sequence of types, and sorting them according to the distance (or rather the index) of the types. You may need to put some more types in there according to what you expect from your clients. Your other option is providing the less<> types needed for sort<> to know the ordering relation between types. Good luck, Rodrigo [1] http://lists.boost.org/boost-users/2008/03/34797.php
On 10/14/2013 4:28 PM, Rodrigo Madera wrote:
Then again, taking a deeper look on your snippet, you are sorting all kinds of types.
I remember seeing something similar in a thread here and found it at [1].
It works by creating an ascending sequence of types, and sorting them according to the distance (or rather the index) of the types. You may need to put some more types in there according to what you expect from your clients.
Your other option is providing the less<> types needed for sort<> to know the ordering relation between types.
I ended up just creating a set from the types instead.
typedef typename fold< vector
participants (3)
-
Gordon Woodhull
-
Michael Marcin
-
Rodrigo Madera