Consider the following code:
template <unsigned int N>
struct myclass
{
unsigned int f() {return N;}
unsigned int g() {static_assert(N > 0, ""); return N-1;}
};
Question: Do I have the guarantee that the following code will compile:
myclass<0> c;
c.f();
But the following will not:
myclass<0> c;
c.f();
c.g();
Yes, you have that guarantee. From [temp.inst]/11, emphasis mine:
If you don't call
g()
, it doesn't require instantiation, so there should be no issues callingmyclass<0>{}.f()
.This is the same guarantee that lets you use
std::vector
andstd::map
with types that aren't default constructible as long as you don't do things like callresize()
andoperator[]
, respectively.A followup, as Jarod42 points out, is that explicitly instantiating
myclass<0>
will produce the assert because, from [temp.explicit]/8:The exceptions don't apply here.