This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Visual Studio 2010 and templating size-of-vector

Tags: None
(comma "," separated)
dharmon
Registered Member
Posts
6
Karma
0
Hi,

I'm having some strange issues in Visual Studio 2010 with code that works fine in gcc and clang.
I'm wondering if anyone else has experienced this.

Basically, I want to template a recursive function by the size of a vector, and then specialize the
base case.

Code: Select all
namespace proj
{

template <int N>
size_t foo(const Eigen::Matrix<Real, N, 1> &in, Eigen::Matrix<Real, N-1, 1> &out)
{
  Eigen::Matrix<Real, N-1, 1> newIn = bar(in); // some other function

  Eigen::Matrix<Real, N-2, 1> newOut;
  size_t ret = foo<N-1>(newIn, newOut); // recursive call

  size_t nbr=0;
  // do some stuff with ret, newOut to construct the entries in out and get nbr

  return nbr;
}

// Template specialization for N=2
//
template <>
size_t foo<2>(const Eigen::Matrix<Real, 2, 1> &in, Eigen::Matrix<Real, 1, 1> &out)
{
  // do something specifically for the N=2 case
}

}


The original is code is more involved, so I distilled it to the essence of what is generating the problem,
namely the following error in VS2010:

"C2912: proj::foo<2>(...) is not a specialization of a function template"

Basically, it looks like it is unable to match the specialized version of foo to the templated version, which
doesn't make sense to me.

I've tried all sorts of things, including templating on two numbers, N and M, in case VS2010 doesn't like
using "N-1" inside the function signature?

Any clues as to what's happening here? Any help would be much appreciated.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
The template specialization should look like:

Code: Select all
template <> size_t foo(const Eigen::Matrix<Real, 2, 1> &in, ...) { ... }
dharmon
Registered Member
Posts
6
Karma
0
You mean by getting rid of the <2>? It shouldn't make a difference (and it fact, it doesn't. I still
get the same error in VS2010).

I am unable to re-create this problem without using Eigen*, so that's why I posted here, in case
it is a known thing when working with Eigen.

Maybe it's just applying templates in a different order than gcc/clang. So that by the time this
specialization is done, it sees parameters of Matrix<Real, 3, 1> and Matrix<Real, 2, 1>, which
although derived from the same class, are distinct classes and make the function parameters
not match.

I think I can hack a workaround by passing the underlying pointers from data() and then re-wrapping
them into an Eigen container, so that no Eigen object appears as a parameter. It's ugly but
should work.

Thanks.


*I wrote a stupid test function to compute a factorial, but the input number is templated. It follows
the same outline as above, but compiles and works fine in VS2010. If I add an Eigen Matrix parameter
like above (even though it doesn't do anything), it won't compile anymore.
Code: Select all
template <int N> int fact() { return N * fact<N-1>(); }
template <> int fact<0>() { return 1; }


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]