Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class template having pointers arguments with default value

Tags:

c++

templates

I am trying to understand the below code snippet

template <typename T>
class testopaque {
public:
    void test(T var = T()) {}
};

How does the default argument work when called with a pointer type example int *

int main() {
    testopaque<int *> obj1;
    obj1.test();
}

what would the compiler generate when obj1.test() is called. I get a compiler error when I try

int main() {
    int * var = int *();
}

error: expected primary-expression before ‘int’
int * ptr = int *();
like image 678
bharat Avatar asked Dec 30 '21 14:12

bharat


People also ask

Can template parameters have default values?

Just like in case of the function arguments, template parameters can have their default values. All template parameters with a default value have to be declared at the end of the template parameter list.

Can default argument be used with the template class?

Can default arguments be used with the template class? Explanation: The template class can use default arguments.

What is the default value of a pointer variable?

A pointer stores the memory address of a value (variable). The default value of a pointer in the Go programming language is nil.

Can template type be a pointer?

A template has only one type, but a specialization is needed for pointer, reference, pointer to member, or function pointer types. The specialization itself is still a template on the type pointed to or referenced.

Can a template class have a default argument?

A template class can have a default argument associated with a generic type. template < class X= int > class myclass { //... Here, the type int will be used if no other type is specified when an object of type myclass is instantiated. It is also permissible for non-type arguments to take default arguments.

Can template parameters have default values?

Like function default arguments, templates can also have default arguments. For example, in the following program, the second parameter U has the default value as char. Also, similar to default function arguments, if one template parameter has a default argument, then all template parameters following it must also have default arguments.

Can a non-type parameter have a default value?

It is also permissible for non-type arguments to take default arguments. The default value is used when no explicit value is specified when the class is instantiated. Default arguments for non-type parameters are specified by using the same syntax as default arguments for function parameters.

What is a template parameter in C++?

An identifier that names a non-type template parameter of class type T denotes a static storage duration object of type const T, called a template parameter object, whose value is that of the corresponding template argument after it has been converted to the type of the template parameter.


3 Answers

Suppose you have x=1+2. Would you expect x*3, which is 9, to equal 1+2*3, which is 7?

A similar problem is happening here. int*() isn't the same as T=int* then T().

Try (int*){}, which solves the combined parsing and precident problems. Or using T=int*; int* x=T();, or even int*x={};

(Simply using (int*)() doesn't solve the problem due to how types are parsed, which is probably something you don't want to go too deeply into if you value your sanity.)

like image 58
Yakk - Adam Nevraumont Avatar answered Oct 18 '22 17:10

Yakk - Adam Nevraumont


This is an example of how C++'s complicated syntax and grammar just produces unexpected results:

int *();

Your C++ compiler is very tempted to interpret this construct as a "function returning a pointer to an int". Your C++ compiler gives in to this temptation, with the observed results.

You need to teach your C++ compiler what you're trying to accomplish here:

typedef int *intp;

int main()
{
    int * var = intp();
}
like image 33
Sam Varshavchik Avatar answered Oct 18 '22 17:10

Sam Varshavchik


When you use an expression like:

T var = T();

Where T is a pointer type then var will be assigned nullptr

like image 28
Sean Avatar answered Oct 18 '22 19:10

Sean