1

For example, suppose I have a template class foo in foo.h like below:

template <typename T>
struct foo {
    constexpr static auto value = false;
};

in first.cpp file, I use the foo like below:

#include "foo.h"
auto dummy1() {
    return foo<int>::value;
}

in second.cpp file, I write partial specialization for foo and use foo like below:

#include "foo.h"
template <std::integral T>
struct foo<T> {
    constexpr static auto value = true;
};

auto dummy2() {
    return foo<int>::value;
}

It's such program a well defined behavior in c++? What if dummy2 use unsigned int like below:

auto dummy2() {
    return foo<unsigned int>::value;
}
macomphy
  • 413
  • 1
  • 9
  • First things first, that is an "explicit specialization" and not a "partial specialization" – Jason Nov 30 '22 at 02:57
  • 1
    See dupe [Explicit specialization of member function template in source file](https://stackoverflow.com/questions/30530364/explicit-specialization-of-member-function-template-in-source-file) – Jason Nov 30 '22 at 03:00
  • sorry, I have updated problem now. @JasonLiam – macomphy Nov 30 '22 at 03:03
  • See [Is partial specialization in a cpp file not "well-formed"](https://stackoverflow.com/questions/31755029/is-partial-specialization-in-a-cpp-file-not-well-formed/31755465#31755465) – Jason Nov 30 '22 at 03:06

1 Answers1

1

That's explicit speciliaztion, not partial specialization, and yes it makes the program IFNDR (ill-formed, no diagnostic required) which effectively means the same as undefined behavior for all inputs.

See [temp.expl.spec]/7 which requires the explicit specialization to be declared before first use that would cause implicit instantiation of the specialization in every translation unit where such use is present.


After the update of the question:

Now it is indeed a partial specialization, but basically the same holds. See [temp.class.spec.general]/1 for the equivalent sentence regarding partial specializations.

So you need to at least add a declaration of the partial specialization to the header:

template <typename T>
struct foo {
    constexpr static auto value = false;
};

template <std::integral T>
struct foo<T>;

Although then it won't be enough to resolve foo<int>::value, so you are back to needing to put the whole definition of the partial specialization into the header.

user17732522
  • 53,019
  • 2
  • 56
  • 105