0

Consider the following existing code (which compiles and executes as expected):

/* File foo.h */

extern const struct Foo bar[]; /* Definition in foo.cpp */

struct Foo
{
    Foo(int i) : Foo(bar[i]) {}
    int x;
};

I now want to change Foo to a template class, such that:

template <typename T>
struct Foo
{
    Foo(int i) : Foo(bar[i]) {}
    T x;
};

How do I need to declare extern const struct Foo bar[] now so that the code will compile?

1 Answer 1

1
  • First, forward declare template <typename T> struct Foo.
  • Then, declare bar using the forward declared Foo.

template <typename T>
struct Foo;

extern const Foo<int> bar[];

template <typename T>
struct Foo
{
    Foo(int i) : Foo(bar[i]) {}
    T x;
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.