b ()' in main () compiles, but the string...">

"Expected Primary Expression Error" does not use dependent type

In the following code, the string 'bObj-> b ()' in main () compiles, but the string 'cObj-> c ()' gives the expected primary expression before '>' token '. This is not related to dependent types; adding a "template" or "typename" to the appropriate location does not help. Any hint what the problem is? In the "real" version of this code, there are many other templates and non-teratic versions of the "b" function. The problem can occur only in one place and not in others, but I could not determine what could be an important difference.

#include <boost/shared_ptr.hpp> class A {}; class D : public A {}; class B { public: template <class T> boost::shared_ptr<T> b() { return boost::shared_ptr<T>(); } }; class C { public: boost::shared_ptr<A> b() { return boost::shared_ptr<A>(); } }; int main(int, char **) { boost::shared_ptr<C> cObj(new C); boost::shared_ptr<B> bObj = boost::dynamic_pointer_cast<B>(cObj); bObj->b<D>(); cObj->b<D>(); } 
+4
source share
1 answer

C does not have a member function template, but only a regular member function. Therefore, you cannot provide template arguments when calling C::b() :

 cObj->b<D>(); // ERROR! cObj->b(); // OK 

Wherein:

In the "real" version of this code, there are many other templates and non-teratic versions of the "b" function.

Then, most likely, the code you provided is not a good example of the problem you have in the "real" version of your code.

The problem can occur only in one place and not in others, but I could not determine what could be an important difference.

You know your code best. We cannot make assumptions about what we do not see. If this answer does not solve your problem, I think you should provide a more representative example of what your "real" code does, possibly reducing it to SSCCE .

+4
source

All Articles