Can we have an anonymous struct as template argument?

Posted by nonoitall on Stack Overflow See other posts from Stack Overflow or by nonoitall
Published on 2011-01-01T03:28:03Z Indexed on 2011/01/01 4:53 UTC
Read the original article Hit count: 203

The title is pretty self-explanatory, but here's a simplified example:

#include <cstdio>

template <typename T>
struct MyTemplate {

    T member;

    void printMemberSize() {
        printf("%i\n", sizeof(T));
    }

};

int main() {

    MyTemplate<struct { int a; int b; }> t; // <-- compiler doesn't like this

    t.printMemberSize();

    return 0;

}

The compiler complains when I try to use an anonymous struct as a template argument. What's the best way to achieve something like this without having to have a separate, named struct definition?

© Stack Overflow or respective owner

Related posts about c++

Related posts about templates