|
-
November 21st, 2012, 05:50 AM
#4
Re: get return type of function
the easiest way is to write a trait class
Code:
template <class T>
struct grad_type {};
template <>
struct grad_type<Scalar> { typedef Vector type; };
template <>
struct grad_type<Vector> { typedef Tensor type; };
template <class T>
void test(T x) {
typename grad_type<Y>::type Y = grad(x)
}
of course, you need to specialize the trait for each possible parameter type. It would be easier if Scalar,Vector,Tensor had some high-level info in them ( like a rank grading ), in this way the trait could compute the grad_type automatically at compile time.
Anyway, I think emulating "auto"/"decltype" is possible in this case via some template machinery like SFINAE, but I wouldn't advise it if you don't know what you're doing ...
moreover, another simple solution could be to refactor the code, if possible; something like
Code:
template <class T,class G>
void test_impl(T& x,G y) {
// ...
}
template <class T>
void test(T x) {
test_impl( x, grad(x) );
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|