Boost logo

Boost Users :

From: cp now (cppnow_at_[hidden])
Date: 2008-03-15 22:51:32


Hello.

I have a multi_index container that stores (and owns) pointers. It has 4
indexes that order themselves on member functions.

I get error-free and warning-free compiles as long as optimization is turned
off (-O0). I get the warning below at all levels of optimization (1 through
3).

I've stripped the code down to just what seems to be generating the error.

--- BEGIN test.cpp ---

#include <iostream>
#include <typeinfo>
#include <map>
#include <boost/ref.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/key_extractors.hpp>

using namespace boost::multi_index;

typedef boost::reference_wrapper<const std::type_info> TypeInfo;
#if 0
bool operator<(TypeInfo t1, TypeInfo t2) {
    const std::type_info& t = t1;
    return t.before(t2);
}
#endif

namespace std {
template<>
struct less<TypeInfo> {
    bool operator()(const TypeInfo& t1, const TypeInfo& t2) {
        const std::type_info& t = t1;
        return t.before(t2);
    }
};
}

struct NodeBase {
    std::string getID() const {return "blah";}
    TypeInfo getresulttypeinfo() const {return TypeInfo(typeid(int));}
    std::string name() const {return "name";}
};

typedef multi_index_container<
    const NodeBase*,
    indexed_by<
        sequenced<>
        , ordered_unique<
            const_mem_fun<NodeBase, std::string, &NodeBase::getID>
>
        , ordered_non_unique<
            const_mem_fun<NodeBase, TypeInfo, &NodeBase::getresulttypeinfo>
>
        , ordered_non_unique<
            const_mem_fun<NodeBase, std::string, &NodeBase::name>
>
>
> NodeMultiIndex;

int main() {
    NodeMultiIndex c;
    NodeMultiIndex::nth_index<0>::type& index = c.get<0>();
    index.push_back(new NodeBase());
    std::cout << *index.begin() << std::endl;
    return 0;
}

--- END test.cpp ---

Compiling with G++ 4.1.1 with full optimizations and full warnings:

g++-4.1.1 -I../eclipselibs/boost -O3 -Wall -c -fmessage-length=0 test.cpp

../eclipselibs/boost/boost/multi_index/ordered_index.hpp: In member function
'boost::multi_index::detail::ordered_index_node<typename
SuperMeta::type::node_type>*
boost::multi_index::detail::ordered_index<KeyFromValue, Compare, SuperMeta,
TagList, Category>::insert_(typename boost::call_traits<typename
boost::multi_index::detail::ordered_index_node<typename
SuperMeta::type::node_type>::value_type>::param_type,
boost::multi_index::detail::ordered_index_node<typename
SuperMeta::type::node_type>*) [with KeyFromValue =
boost::multi_index::const_mem_fun<NodeBase, std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, &NodeBase::getID>, Compare =
std::less<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > >, SuperMeta =
boost::multi_index::detail::nth_layer<2, const NodeBase*,
boost::multi_index::indexed_by<boost::multi_index::sequenced<boost::multi_index::tag<mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na> >,
boost::multi_index::ordered_unique<boost::multi_index::const_mem_fun<NodeBase,
std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
&NodeBase::getID>, mpl_::na, mpl_::na>,
boost::multi_index::ordered_non_unique<boost::multi_index::const_mem_fun<NodeBase,
boost::reference_wrapper<const std::type_info>,
&NodeBase::getresulttypeinfo>, mpl_::na, mpl_::na>,
boost::multi_index::ordered_non_unique<boost::multi_index::const_mem_fun<NodeBase,
std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
&NodeBase::name>, mpl_::na, mpl_::na>, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>,
std::allocator<const NodeBase*> >, TagList = boost::mpl::vector0<mpl_::na>,
Category = boost::multi_index::detail::ordered_unique_tag]':
../eclipselibs/boost/boost/multi_index/ordered_index.hpp:551: warning:
'inf$side' may be used uninitialized in this function

I can eliminate this warning (for all levels of optimization) with the patch
below.

Index: boost/multi_index/ordered_index.hpp
===================================================================
RCS file: /cvsroot/boost/boost/boost/multi_index/ordered_index.hpp,v
retrieving revision 1.12.2.3
diff -u -r1.12.2.3 ordered_index.hpp
--- boost/multi_index/ordered_index.hpp 26 Feb 2007 09:16:36 -0000
1.12.2.3
+++ boost/multi_index/ordered_index.hpp 16 Mar 2008 01:40:02 -0000
@@ -549,6 +549,7 @@
   node_type* insert_(value_param_type v,node_type* x)
   {
     link_info inf;
+ inf.side = to_left; inf.pos = 0;
     if(!link_point(key(v),inf,Category())){
       return node_type::from_impl(inf.pos);
     }

I imagine something similar would have to be done for the other insert
functions, though my code probably doesn't use them and so the warnings
aren't generated at that line.

Alternatively, the link_info struct could have a default constructor that
initializes the members. Or link_point() etc. could return tuples,
eliminating the need for pass-by-reference, etc.

Is there an easier workaround than patching the library header? Or, worse,
am I doing something wrong?

Thanks

V



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net