title: How to return Template Value from boost::apply_visitor?
body: <p>The following code correctly spits out the values <code>999</code> and "<code>test</code>" to the console but how do I return these values instead? Something like the commented line was my ultimate goal; return the value that I can then assign to an auto variable (since I don't know the type I will be dealing with). I plan to use <code>boost::lexical_cast</code> to convert this value to a <code>std::string</code> to insert it into a field into a database.</p>
  
  <p>I have tried various variations of the visitor from other examples without success, including deriving <code>DataMap</code> from a base class so that I could store the variable as a member. My attempts have ultimately been unsuccessful.</p>
  
  <p>Any suggestions are appreciated.  Thanks.</p>
  
  <pre><code>#include "stdafx.h"
  #include "boost\variant.hpp"
  #include &lt;iostream&gt;
  #include &lt;map&gt;
  
  struct DataMap
  {
      DataMap() {};
  
      typedef std::map&lt;std::string, std::map&lt;std::string,
          boost::variant&lt;int, std::string&gt;&gt;&gt; ArtifactMap;
  
      ArtifactMap::const_iterator begin() const { return _data.begin(); }
      ArtifactMap::const_iterator end() const { return _data.end(); }
      ArtifactMap _data;
  };
  
  struct DataMapVisitor : public boost::static_visitor&lt;&gt;
  {
      template&lt;typename T&gt;
      void operator()(const T&amp; t) const { std::cout &lt;&lt; t &lt;&lt; std::endl; }
  };
  
  
  int _tmain(int argc, _TCHAR* argv[])
  {
      DataMap dataMap;
      std::map&lt;std::string, boost::variant&lt;int, std::string&gt;&gt; columns;
      columns.insert(std::make_pair("Col1", 999));
      columns.insert(std::make_pair("Col2", "test"));
      dataMap._data.insert(std::make_pair("Table1", columns));
  
      for (auto table : dataMap)
      {
          for (auto column : table.second)
          {
             boost::apply_visitor(DataMapVisitor(), column.second);
             //auto value = boost::apply_visitor(DataMapVisitor(), column.second);
          }
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Edit: As an update, the following code has the visitor properly returning a <code>std::string</code>.  I'm guessing there is no way to have one visitor return multiple value types (i.e., return the <code>string</code> value if it's a <code>string</code>, return the <code>int</code> value if it's an <code>int</code>, etc.)? </p>
  
  <pre><code>class DataMapVisitor : public boost::static_visitor&lt;std::string&gt;
  {
  public:
  
     template&lt;typename T&gt;
     std::string operator()(const T&amp; value) const
     {
        try
        {
           return boost::lexical_cast&lt;std::string&gt;(value);
        }
        catch (boost::bad_lexical_cast&amp;)
        {
           return "";
        }
     }
  };
  </code></pre>