Tony The Lion
Tony The Lion

Reputation: 63190

Template function to return template type

I have the following function but my compiler (VS2003) says that the assignement T = .... is illegal. Can someone clarify what I've done wrong? The type of value is a boost::variant. node is a struct.

 template <typename T>
    T find_attribute(const std::string& attribute)
    {

        std::vector<boost::shared_ptr<node> >::iterator nodes_iter = _request->begin();

        for (; nodes_iter != _request->end(); nodes_iter++)
        {
            std::vector<node::attrib>::iterator att_iter = (*nodes_iter)->attributes.begin();
            for (; att_iter != att_iter; (*nodes_iter)->attributes.end())
            {
                if ((*att_iter).key.compare(attribute) == 0) {
                    T = (*att_iter).value;  //T : Illegal use of this type as an expression.
                                    return T; 
                            }

            }

        }
    }

Upvotes: 0

Views: 2177

Answers (2)

Konrad Rudolph
Konrad Rudolph

Reputation: 545488

T is a type, not a variable name. In the general case, you should declare a variable as mentioned by @sharptooth (this is the usual workflow for any function!).

In your particular case, it’s better just to return the value – no additional variable is needed:

return (*att_iter).value;

or, better yet:

return att_iter->value;

Upvotes: 4

sharptooth
sharptooth

Reputation: 170479

You should declare a variable:

if ((*att_iter).key.compare(attribute) == 0) {
    T temp = (*att_iter).value;  //T : Illegal use of this type as an expression.
    return temp; 
}

Upvotes: 5

Related Questions