Template instantiation - no matching function when using C++11's sizeof…() operator with certain classes

StackOverflow https://stackoverflow.com/questions/9333118

  •  27-10-2019
  •  | 
  •  

Question

Consider the following code (C++11), which uses the Eigen 3 library (http://eigen.tuxfamily.org):

#include <iostream>
#include <Eigen/Core>

template<typename T, int x, int y>
class mat {
        public:
        private:
                T data[x*y];
};

class bazinga {
        public:
                template<typename T>
                static void static_foo() {
                        std::cout << "STATIC BAZINGA FOO!\n";
                }

                template<typename T>
                void foo() {
                        std::cout << "BAZINGA FOO!\n";
                }
};

template<typename T>
void direct_foo() {
        std::cout << "JUST FOO-IN'!\n";
}

template<int i, int ... is>
void bar( bazinga &bz) {

        direct_foo<          mat<float, sizeof...(is), 3> >(); // OK
        bazinga::static_foo< mat<float, sizeof...(is), 3> >(); // OK
        bz.foo<              mat<float, sizeof...(is), 3> >(); // OK

        direct_foo<          Eigen::Matrix<float, sizeof...(is), 3> >(); // OK
        bazinga::static_foo< Eigen::Matrix<float, sizeof...(is), 3> >(); // ERROR
        bz.foo<              Eigen::Matrix<float, sizeof...(is), 3> >(); // ERROR

        bazinga::static_foo< Eigen::Matrix<float, 3, 3> >(); // OK!!!
        bz.foo<              Eigen::Matrix<float, 3, 3> >(); // OK!!!
}

int main() {
        bazinga bz;
        bar<1,2,3,4>(bz);
}

Class "mat" is just there to demonstrate that the errors do not occur using own classes, but only with the Eigen::Matrix class.

Can someone come up with a reason or clues on why this is happening? Or post a solution?

Thank you!

EDIT: i use g++ 4.6.1 on ubuntu 11.10 here is the compiler output:

main.cpp: In function ‘void bar(bazinga&) [with int i = 1, int ...is = {2, 3, 4}]’:
main.cpp:46:24:   instantiated from here
main.cpp:37:9: error: no matching function for call to ‘bazinga::static_foo()’
main.cpp:37:9: note: candidate is:
main.cpp:14:29: note: template<class T> static void bazinga::static_foo()
main.cpp:38:9: error: no matching function for call to ‘bazinga::foo()’
main.cpp:38:9: note: candidate is:
main.cpp:19:22: note: template<class T> void bazinga::foo()

EDIT2: I guess I was not clear enough: what surprises me most is that if i pass a fixed value as second template parameter (as in the last two lines of bar()) everything works fine, only using the sizeof...() operator results in errors, and only when used with Eigen::Matrix.

Was it helpful?

Solution

I think it is a compiler bug, as passing integral literal works fine, but sizeof...(is) doesn't work. So I would suggest you to try this:

constexpr int s = sizeof...(is);
bazinga::static_foo< Eigen::Matrix<float, s, 3> >();
bz.foo<              Eigen::Matrix<float, s, 3> >(); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top