Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template Constraints C++

In C# we can define a generic type that imposes constraints on the types that can be used as the generic parameter. The following example illustrates the usage of generic constraints:

interface IFoo { }   class Foo<T> where T : IFoo { }  class Bar : IFoo { }  class Simpson { }  class Program {     static void Main(string[] args)     {         Foo<Bar> a = new Foo<Bar>();         Foo<Simpson> b = new Foo<Simpson>(); // error CS0309     } } 

Is there a way we can impose constraints for template parameters in C++.


C++0x has native support for this but I am talking about current standard C++.

like image 946
Jorge Ferreira Avatar asked Sep 23 '08 17:09

Jorge Ferreira


People also ask

What is meant by template parameter?

A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.

What is a template in C programming?

Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one.

Why do we use :: template template parameter?

8. Why we use :: template-template parameter? Explanation: It is used to adapt a policy into binary ones.

Which is correct example of template parameters?

For example, given a specialization Stack<int>, “int” is a template argument. Instantiation: This is when the compiler generates a regular class, method, or function by substituting each of the template's parameters with a concrete type.


2 Answers

If you use C++11, you can use static_assert with std::is_base_of for this purpose.

For example,

#include <type_traits>  template<typename T> class YourClass {      YourClass() {         // Compile-time check         static_assert(std::is_base_of<BaseClass, T>::value, "type parameter of this class must derive from BaseClass");          // ...     } } 
like image 125
Venemo Avatar answered Nov 07 '22 12:11

Venemo


"Implicitly" is the correct answer. Templates effectively create a "duck typing" scenario, due to the way in which they are compiled. You can call any functions you want upon a template-typed value, and the only instantiations that will be accepted are those for which that method is defined. For example:

template <class T> int compute_length(T *value) {     return value->length(); } 

We can call this method on a pointer to any type which declares the length() method to return an int. Thusly:

string s = "test"; vector<int> vec; int i = 0;  compute_length(&s); compute_length(&vec); 

...but not on a pointer to a type which does not declare length():

compute_length(&i); 

This third example will not compile.

This works because C++ compiles a new version of the templatized function (or class) for each instantiation. As it performs that compilation, it makes a direct, almost macro-like substitution of the template instantiation into the code prior to type-checking. If everything still works with that template, then compilation proceeds and we eventually arrive at a result. If anything fails (like int* not declaring length()), then we get the dreaded six page template compile-time error.

like image 27
Daniel Spiewak Avatar answered Nov 07 '22 12:11

Daniel Spiewak