Force compile time check for correct object instantiation
Solution 1:
I did read and read your question and comments and maybe I am confused what you want but why simple solution using constexpr constructor does not suit you:
#include <iostream>
struct Polynomial {
const char* str;
constexpr Polynomial(char const* arg)
: str(arg) {
// if check fails when constructing constexpr Polynomial
// we get compile error for throwing, otherwise works
// if check fails when constructing runtime Polynomial
// then it does throw
if (arg[0] != 'b') throw 42;
}
};
int main() {
// should compile as first character is 'b'
constexpr Polynomial a("bar");
const char* foo = "foo";
try {
// should throw as first character is not 'b'
Polynomial b(foo);
} catch (...) {
std::cout << "Q.E.D." << std::endl;
}
}
This AFAIK worked already in C++14.