Questions tagged [definition-checking]

Definition checking is the ability of a compiler to validate a class or function definition against requirements specified on template parameters.

Definition checking gives compiler the ability to prevent a C++ template from using operations that are not specified in the requirements on the type parameters. This feature is not supported by C++20 concepts.

7 questions
17
votes
2 answers

c++ template weird optimization

I wrote a singleton template class just like boost does: template class Singleton { public : static _T* Instance() { static _T obj; return &obj; } protected : Singleton() {} private : struct…
Riopho
  • 211
  • 1
  • 9
11
votes
2 answers

C++ compiler does not check if a method exists in template class

I came across with the following program in C++: template class Val { protected: T x0, x; public: Val(T t = 1) : x0(t), x(1) {} T val() { return x; } void promote() { this->promote_value(); } }; For some reason…
vesii
  • 2,760
  • 4
  • 25
  • 71
8
votes
1 answer

Why do C++ templates match if method doesn't type-check?

The follow code does not compile because struct A doesn't support the -- operator. struct A {}; struct B { void Run() {} A& Dec(A& a) { return --a; } }; int main(int argc, char** argv) { B b; b.Run(); } Same with this code. struct A…
5
votes
3 answers

SFINAE automatically check that function body compiles without explicit constraints

I often use SFINAE to remove a function from the overload set if the function body does not make sense(i.e. does not compile). Would it be possible to add to C++ a simple require statement? For example, let's have a function: template T…
tom
  • 1,520
  • 1
  • 12
  • 26
4
votes
2 answers

How do I statically check my templated class at definition time?

In C# or Java, the following does not compile, because I "forgot" the where part in class declaration, that specifies that T is instance of something that defines the add method. class C { T make(T t) { return t.add(t); } } I'd…
user7610
  • 25,267
  • 15
  • 124
  • 150
1
vote
2 answers

C++20: Validate Template Bodies Against Concepts

C++20 introduces concepts, which allows us to specify in the declaration of a template that the template parameters must provide certain capabilities. If a template is instantiated with a type that does not satisfy the constraints, compilation will…
1
vote
2 answers

Are C++ templates be checked for syntax errors without instantiating them?

If a C++ template contains a blatant type error, such as a reference to a class member that does not exist, does the language standard guarantee to detect the error at the time the template is defined? Or is the error guaranteed to be detected…
Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533