Probleme mit Boost Variant und Templates



  • Hallo,

    ich habe folgende Definition eines Function Members eines Klassentemplates:

    template<class N, class PN, class C, class CC, class RC, class PC, class T> PN* Base_Nd<N, PN, C, CC, RC, PC, T>::parent_node() const
    {
    	class my_visitor : public boost::static_visitor<int>
    	{
    	public:
    	    int operator()(int i) const
    	    {
    	        return i;
    	    }
    
    	    int operator()(const std::string & str) const
    	    {
    	        return str.length();
    	    }
    	};
    
    	boost::variant< int, std::string > u("hello world");
    	int result = boost::apply_visitor( my_visitor(), u );
    }
    

    Wenn ich das versuche zu kompilieren, erhalte ich die Fehlermeldung

    no matching function for call to ‘apply_visitor(Base_Nd<N, PN, C, CC, RC, PC, T>::parent_node() const [with N = Rev_Nd, PN = Rev_Intern_Nd, C = Rev_NC, CC = Rev_Coal_NC, RC = Rev_Rec_NC, PC = Rev_Intern_NC, T = Tree]::my_visitor, boost::variant<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_>&)’

    bezüglich

    int result = boost::apply_visitor( my_visitor(), u );
    

    Wenn ich allerdings my_visitor aus der Funktionsdefinition rausziehe, d. h.

    class my_visitor : public boost::static_visitor<int>
    {
    public:
        int operator()(int i) const
        {
            return i;
        }
    
        int operator()(const std::string & str) const
        {
            return str.length();
        }
    };
    
    template<class N, class PN, class C, class CC, class RC, class PC, class T> PN* Base_Nd<N, PN, C, CC, RC, PC, T>::parent_node() const
    {
    	boost::variant< int, std::string > u("hello world");
    	int result = boost::apply_visitor( my_visitor(), u );
    }
    

    kann ich fehlerfrei kompilieren. Weiss jemand, was ich tun muss, um my_visitor in die Funktionsdefinition packen zu können?



  • ingobulla schrieb:

    Weiss jemand, was ich tun muss, um my_visitor in die Funktionsdefinition packen zu können?

    Den Standard ändern. Sprich: Das ist nicht erlaubt.



  • Gegen welche Regel verstoße ich denn hier?



  • ingobulla schrieb:

    Gegen welche Regel verstoße ich denn hier?

    dass template parameter external linkage haben müssen



  • Shade Of Mine schrieb:

    dass template parameter external linkage haben müssen

    Trotz längerer Reflektionsphase war ich nicht in der Lage, das in Bezug auf meinen Code zu verstehen. Kann das irgendwer erläutern?



  • You can use a local class in most places where you can use a nonlocal class, but there is a major restriction to keep in mind: A local or unnamed class cannot be used as a template parameter. From the C++ standard (14.3.1/2):

    Das obige Zitat stammt von dieser Seite, Punkt 2.
    http://www.gotw.ca/gotw/058.htm

    Simon


Anmelden zum Antworten