<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <meta http-equiv="X-UA-Compatible" content="IE=9"/> <meta name="generator" content="Doxygen 1.8.5"/> <title>Eigen: Using STL Containers with Eigen</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="dynsections.js"></script> <link href="navtree.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="resize.js"></script> <script type="text/javascript" src="navtree.js"></script> <script type="text/javascript"> $(document).ready(initResizable); $(window).load(resizeHeight); </script> <link href="search/search.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="search/search.js"></script> <script type="text/javascript"> $(document).ready(function() { searchBox.OnSelectItem(0); }); </script> <link href="doxygen.css" rel="stylesheet" type="text/css" /> <link href="eigendoxy.css" rel="stylesheet" type="text/css"> <!-- --> <script type="text/javascript" src="eigen_navtree_hacks.js"></script> <!-- <script type="text/javascript"> --> <!-- </script> --> </head> <body> <div id="top"><!-- do not remove this div, it is closed by doxygen! --> <!-- <a name="top"></a> --> <div id="titlearea"> <table cellspacing="0" cellpadding="0"> <tbody> <tr style="height: 56px;"> <td id="projectlogo"><img alt="Logo" src="Eigen_Silly_Professor_64x64.png"/></td> <td style="padding-left: 0.5em;"> <div id="projectname"><a href="http://eigen.tuxfamily.org">Eigen</a>  <span id="projectnumber">3.2.0</span> </div> </td> <td> <div id="MSearchBox" class="MSearchBoxInactive"> <span class="left"> <img id="MSearchSelect" src="search/mag_sel.png" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()" alt=""/> <input type="text" id="MSearchField" value="Search" accesskey="S" onfocus="searchBox.OnSearchFieldFocus(true)" onblur="searchBox.OnSearchFieldFocus(false)" onkeyup="searchBox.OnSearchFieldChange(event)"/> </span><span class="right"> <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a> </span> </div> </td> </tr> </tbody> </table> </div> <!-- end header part --> <!-- Generated by Doxygen 1.8.5 --> <script type="text/javascript"> var searchBox = new SearchBox("searchBox", "search",false,'Search'); </script> </div><!-- top --> <div id="side-nav" class="ui-resizable side-nav-resizable"> <div id="nav-tree"> <div id="nav-tree-contents"> <div id="nav-sync" class="sync"></div> </div> </div> <div id="splitbar" style="-moz-user-select:none;" class="ui-resizable-handle"> </div> </div> <script type="text/javascript"> $(document).ready(function(){initNavTree('group__TopicStlContainers.html','');}); </script> <div id="doc-content"> <!-- window showing the filter options --> <div id="MSearchSelectWindow" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()" onkeydown="return searchBox.OnSearchSelectKey(event)"> <a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark"> </span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark"> </span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark"> </span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark"> </span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark"> </span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark"> </span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark"> </span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark"> </span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark"> </span>Friends</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark"> </span>Groups</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark"> </span>Pages</a></div> <!-- iframe showing the search results (closed by default) --> <div id="MSearchResultsWindow"> <iframe src="javascript:void(0)" frameborder="0" name="MSearchResults" id="MSearchResults"> </iframe> </div> <div class="header"> <div class="headertitle"> <div class="title">Using STL Containers with Eigen<div class="ingroups"><a class="el" href="group__DenseMatrixManipulation__Alignement.html">Alignment issues</a></div></div> </div> </div><!--header--> <div class="contents"> <h1><a class="anchor" id="summary"></a> Executive Summary</h1> <p>Using STL containers on <a class="el" href="group__TopicFixedSizeVectorizable.html">fixed-size vectorizable Eigen types</a>, or classes having members of such types, requires taking the following two steps:</p> <ul> <li>A 16-byte-aligned allocator must be used. <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library. ">Eigen</a> does provide one ready for use: <a class="el" href="classEigen_1_1aligned__allocator.html" title="STL compatible allocator to use with with 16 byte aligned types. ">aligned_allocator</a>. </li> <li>If you want to use the std::vector container, you need to #include <Eigen/StdVector>.</li> </ul> <p>These issues arise only with <a class="el" href="group__TopicFixedSizeVectorizable.html">fixed-size vectorizable Eigen types</a> and <a class="el" href="group__TopicStructHavingEigenMembers.html">structures having such Eigen objects as member</a>. For other <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library. ">Eigen</a> types, such as Vector3f or MatrixXd, no special care is needed when using STL containers.</p> <h1><a class="anchor" id="allocator"></a> Using an aligned allocator</h1> <p>STL containers take an optional template parameter, the allocator type. When using STL containers on <a class="el" href="group__TopicFixedSizeVectorizable.html">fixed-size vectorizable Eigen types</a>, you need tell the container to use an allocator that will always allocate memory at 16-byte-aligned locations. Fortunately, <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library. ">Eigen</a> does provide such an allocator: <a class="el" href="classEigen_1_1aligned__allocator.html" title="STL compatible allocator to use with with 16 byte aligned types. ">Eigen::aligned_allocator</a>.</p> <p>For example, instead of </p> <div class="fragment"><div class="line">std::map<int, Eigen::Vector4f></div> </div><!-- fragment --><p> you need to use </p> <div class="fragment"><div class="line">std::map<int, Eigen::Vector4f, std::less<int>, </div> <div class="line"> <a class="code" href="classEigen_1_1aligned__allocator.html">Eigen::aligned_allocator<std::pair<const int, Eigen::Vector4f></a> > ></div> </div><!-- fragment --><p> Note that the third parameter "std::less<int>" is just the default value, but we have to include it because we want to specify the fourth parameter, which is the allocator type.</p> <h1><a class="anchor" id="vector"></a> The case of std::vector</h1> <p>The situation with std::vector was even worse (explanation below) so we had to specialize it for the <a class="el" href="classEigen_1_1aligned__allocator.html" title="STL compatible allocator to use with with 16 byte aligned types. ">Eigen::aligned_allocator</a> type. In practice you <b>must</b> use the <a class="el" href="classEigen_1_1aligned__allocator.html" title="STL compatible allocator to use with with 16 byte aligned types. ">Eigen::aligned_allocator</a> (not another aligned allocator), <b>and</b> #include <Eigen/StdVector>.</p> <p>Here is an example: </p> <div class="fragment"><div class="line"><span class="preprocessor">#include<Eigen/StdVector></span></div> <div class="line"><span class="comment">/* ... */</span></div> <div class="line">std::vector<Eigen::Vector4f,Eigen::aligned_allocator<Eigen::Vector4f> ></div> </div><!-- fragment --><h2><a class="anchor" id="vector_spec"></a> An alternative - specializing std::vector for Eigen types</h2> <p>As an alternative to the recommended approach described above, you have the option to specialize std::vector for <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library. ">Eigen</a> types requiring alignment. The advantage is that you won't need to declare std::vector all over with Eigen::allocator. One drawback on the other hand side is that the specialization needs to be defined before all code pieces in which e.g. std::vector<Vector2d> is used. Otherwise, without knowing the specialization the compiler will compile that particular instance with the default std::allocator and you program is most likely to crash.</p> <p>Here is an example: </p> <div class="fragment"><div class="line"><span class="preprocessor">#include<Eigen/StdVector></span></div> <div class="line"><span class="comment">/* ... */</span></div> <div class="line">EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(<a class="code" href="group__matrixtypedefs.html#ga5521d9f6a8d61d3f04a819b799ad9a65">Matrix2d</a>)</div> <div class="line">std::vector<Eigen::<a class="code" href="group__matrixtypedefs.html#ga685d563d586f4820b4a2df9a07d98c23">Vector2d</a>></div> </div><!-- fragment --><p><span class="note"><b>Explanation:</b> The resize() method of std::vector takes a value_type argument (defaulting to value_type()). So with std::vector<Eigen::Vector4f>, some <a class="el" href="group__matrixtypedefs.html#ga96452a1b0400ea4ef7935e06914a56b1">Eigen::Vector4f</a> objects will be passed by value, which discards any alignment modifiers, so a <a class="el" href="group__matrixtypedefs.html#ga96452a1b0400ea4ef7935e06914a56b1">Eigen::Vector4f</a> can be created at an unaligned location. In order to avoid that, the only solution we saw was to specialize std::vector to make it work on a slight modification of, here, <a class="el" href="group__matrixtypedefs.html#ga96452a1b0400ea4ef7935e06914a56b1">Eigen::Vector4f</a>, that is able to deal properly with this situation. </span> </p> </div><!-- contents --> </div><!-- doc-content --> <!-- start footer part --> <div id="nav-path" class="navpath"><!-- id is needed for treeview function! --> <ul> <li class="footer">Generated on Mon Oct 28 2013 11:04:27 for Eigen by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.5 </li> </ul> </div> <!-- Piwik --> <!-- <script type="text/javascript"> var pkBaseURL = (("https:" == document.location.protocol) ? "https://stats.sylphide-consulting.com/piwik/" : "http://stats.sylphide-consulting.com/piwik/"); document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E")); </script><script type="text/javascript"> try { var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 20); piwikTracker.trackPageView(); piwikTracker.enableLinkTracking(); } catch( err ) {} </script><noscript><p><img src="http://stats.sylphide-consulting.com/piwik/piwik.php?idsite=20" style="border:0" alt="" /></p></noscript> --> <!-- End Piwik Tracking Code --> </body> </html>