Home >Backend Development >C++ >Can a Non-Template Class Have a Parameterless Template Constructor?

Can a Non-Template Class Have a Parameterless Template Constructor?

Barbara Streisand
Barbara StreisandOriginal
2024-12-26 18:54:14291browse

Can a Non-Template Class Have a Parameterless Template Constructor?

Is Template Constructor Without Parameter Possible?

Question:

Can a non-template class have a template constructor with no arguments? Is it true that it conflicts with the default constructor?

Answer:

Explicitly specifying template arguments is not possible when calling a constructor template. They are deduced through argument deduction. Calling Foo() assigns the template argument list to the type Foo, not to the constructor.

The workaround you suggested still requires an argument passed to call the constructor template. It's unclear what your intended goal is in this scenario.

An alternative is to use a default constructor instead of a template constructor with no arguments. If the non-template class has other constructors, using a default template constructor with empty parentheses () can avoid parameter ambiguity.

The above is the detailed content of Can a Non-Template Class Have a Parameterless Template Constructor?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn