2016-06-17 20 views
-4

Ich benutze boost::any, und habe eine Funktion, die einen solchen Wert abruft, aber kann scheitern, so dass es tatsächlich std::optional<boost::any> (gut, für jetzt ist es std::experimental::optional) zurückgibt. Jetzt, ohne das optionale, bekomme ich meinen typisierten Wert zurück unter Verwendung boost::any_cast(my_retrieved_any). Um den optionalen Fall zu behandeln, habe ich folgendes geschrieben:boost :: any_cast und std :: optional ist

template<typename ValueType> 
inline const optional<ValueType> boost_any_cast(const optional<boost::any>& operand) 
{ 
    return operand ? boost::any_cast(operand.value()) : nullopt; 
} 

Aber das lässt sich nicht kompilieren (mit Boost-1.58 und GCC 4.9.3). Ich bekomme:

/file.cpp(12): error: no instance of overloaded function "boost::any_cast" 
matches the argument list 
      argument types are: (const boost::any) 

Wie ist das möglich? Warum ist das Argument nicht boost::any&? Ich habe versucht, eine variable Einstellung operand.value(), dann ist das auf die any_cast vorbei - aber das schien nicht zu helfen, entweder:

template<typename ValueType> 
inline const optional<ValueType> boost_any_cast(const optional<boost::any>& operand) 
{ 
    if (operand) { 
     boost::any x(operand.value()); 
     return boost::any_cast(x); 
    } 
    return nullopt; 
} 

Das bekommt mir:

/file.cpp(13): error: no instance of overloaded function "boost::any_cast" 
matches the argument list 
      argument types are: (boost::any) 

Es muss sein etwas, das ich bezüglich boost::any 's nicht in Betracht ziehe ... was ist es? Und wie kann ich diese "Casting" -Operation reparieren?

+0

@uhohsomebodyneedsapupper: Siehe Bearbeiten. – einpoklum

+0

Welcher Typ ist diese 'Liste'? Könnten Sie Code bereitstellen, der 'boost_any_cast()' aufruft? –

+0

@ JanKorous: Es gibt keine Liste im Code, es ist die "Argumentliste" der Funktion. Und der Code, der boost_any cast() aufruft, lautet 'auto x = boost_any_cast (function_returning_optional_boost_any())'; – einpoklum

Antwort

3

Die boost::any_cast erfordert ein Vorlageargument;

template<typename T> T any_cast(const any &); 

Von Ihnen Code-Snippet, müssen Sie wahrscheinlich;

boost::any_cast<ValueType>(operand.value())