Skip to main content
gamedev.net
🔒 Locked

Boost::Static_Visitor

Started by ETFBlade 3 replies 1.1k views
Original Post
ETFBlade
ETFBlade
Is it possible to create a boost::static_visitor object that contains it's own data as well? i.e.

template <typename T>
class SomeVisitor : public boost::static_visitor<>
{
public:
    SomeVisitor(T& data) : m_data(data){}
    
    template <typename U>
    void operator()(U& val) { val = m_data; }

private:
    T m_data;
}

then later...

SomeVisitor visitor<int>(1);
boost::apply_visitor( visitor(), some_variant );

Deyja
Deyja
Why wouldn't it? It's a perfectly normal class.

For that matter, what use would a visitor to, say, count the number of visited elements that are blue be if it couldn't store a count?
ETFBlade
ETFBlade
Well,maybe it's something I'm doing wrong, but I get errors when I try to do the above.

I have a Set function, to set the value of a variable. Variables are stored as pointers in a hash_map I have.

	template <typename V>	void	SetVar(const std::string& str, const V& data) { variables[str].Set(data); }private:	stdext::hash_map< std::string, fScriptGlobal >	variables;


The fScriptGlobal is the class that contains the pointer to my variable:

class fScriptGlobal{public:	fScriptGlobal(){}	template <typename T>	fScriptGlobal( T val ) : m_var(val) {}	~fScriptGlobal(){}	template <typename V>	void	Set( const V& val )	{ 		RefVisitor visitor<V>(val);		boost::apply_visitor( visitor, m_var); 	}	variant_var&	Get(){ return m_var;}		// returns a pointer to the data associated with this variableprivate:	variant_var		m_var;};


where variant_var is just a boost::variant containing pointers to some types.

Now I defined my visitor as:

template <typename V>class RefVisitor : public boost::static_visitor<>{public:	RefVisitor(){}	RefVisitor(V& val) : new_val(val){}	template <typename T>	void operator()( T& data ) const	{		*data = new_val;	}private:	V new_val;};


So, essentially, I'm just trying to dereference my variant's pointer, and set its value with this visitor. So, in my driver I try:

int main(){	int i = 10;	externals.RegisterVar( "i", &i ); // externals is defined above main	std::cout << externals.GetVar("i") << std::endl;	i = 20;	std::cout << externals.GetVar("i") << std::endl;		externals.SetVar("i", 10);	std::cout << externals.GetVar("i") << std::endl;	return 0;}


But, before I even get to test this. I get compiler errors.
error C2955: 'RefVisitor' : use of class template requires template argument list - refers to the RefVisitor instance created in Set() up above


Not quite sure what I'm doing wrong here.


Enigma
Enigma
RefVisitor visitor(val);

Σnigma
ETFBlade
ETFBlade
Ah. Thanks Enigma.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.