#error C2908: explicit specialization;

13 messages · Page 1 of 1 (latest)

cosmic ibex
#

How do I make it that I can define classes later in the code?

//in a library build before
template<size_t S>
struct Reflector {
    using Type = GameComponent;
};
// in a library build after
template<> struct Reflector<0> {
    using Type = Transformation3D;
};
template<> struct Reflector<1> {
    using Type = Transformation2D;
};
graceful berryBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

cosmic ibex
#

I want to override in a way these structs or tell the programm i will define them later:

template<> struct Reflector<0>;
template<> struct Reflector<1>;

This approach causes an error since the first library never gets the data before finishing its build.

abstract prism
cosmic ibex
green plank
# cosmic ibex How do I make it that I can define classes later in the code? ```cpp //in a libr...

the way you specialize something like this is:

// empty definition since we don't know what the
// specializations will do
template<size_t S>
struct Reflector;


// specializations
template<> struct Reflector<0> {
    using Type = Transformation3D;
};
template<> struct Reflector<1> {
    using Type = Transformation2D;
};
```what you can't do is use a specialization of `Reflector` before its defined, this still follows the normal c++ rules of only using things after theyre defined

But what you *can* do it use templates that are instantiated later

```cpp
template<size_t S>
struct Reflector;

// this looks like its using `Reflector<S>`
// before its defined but actually only gets
// created when its instantiated
template<size_t S>
typename Reflector<S>::Type foo() {
  return {};
}

// specializations
template<> struct Reflector<0> {
    using Type = Transformation3D;
};
template<> struct Reflector<1> {
    using Type = Transformation2D;
};

int main() {
  Transformation3D t = foo<0>();// instantiation happens here
  // by this point `Reflector` is defined so this is legal
}
#

you also can't magically tell Reflector in another cpp file that Reflector<0> means something without just defining it

#
// a.cpp
template<size_t S>
struct Reflector;

template<> struct Reflector<0> {
    using Type = Transformation3D;
};

// b.cpp
template<size_t S>
struct Reflector;

Reflector<0> r; // undefined class error
#

you also have to be careful because you can easily introduce ODR violations

// a.cpp
template<size_t S>
struct Reflector;

template<> struct Reflector<0> {
    using Type = A
};

// b.cpp
template<size_t S>
struct Reflector;

template<> struct Reflector<0> {
    using Type = B;// oops now its different, this is an ODR violation if these 2 files are linked
};
#

this leads to me favourite quote in the standard

cosmic ibex
#

I just got it to work thank you so much
I still have a lot to learn about this:
thank you so much!

#

!solved