Template Specializations vs. function overloading

简介:

As convention, I prefer to pass the object as const reference below.

void foo(const A& a)

In most of cases, normal conversions are applied to the arguments when the client invokes the function. For example.

A a;
c.foo(a);

Object a will convert to const reference of class A.

 

However, when we use template specializations, conversions are not applied to argument types. In a call to specialized version of a template, the argument types in the call must match the specialized version function parameter type(s) exactly. If they don't, then the complier will instantiate an instantiation for the argument(s) from the template definition.

class A
{
};

class C
{
public:
template<typename T>
void foo(T t)
{
printf("void foo(T t)\n");
}

template<>
void foo(const int i)
{
printf("void foo(int i)\n");
}

template<>
void foo(const A& a)
{
printf("void foo(const A a)\n");
}
};

TEST(TemplateTest, TemplateSpecializationTest)
{
C c;
int i = 2;
c.foo(i);

A a;
c.foo(a);
}

image

Because the parameter to call foo is (A a) install of (const A& a), the complier pick the template version void foo(T t) instead of specialized version void foo(const A& a) .

 

So, in such case, I prefer to use function overloading instead of template specializations.

class A
{
};

class C
{
public:
template<typename T>
void foo(T t)
{
printf("void foo(T t)\n");
}

template<>
void foo(const int i)
{
printf("void foo(const int i)\n");
}

void foo(const A& a)
{
printf("void foo(const A a)\n");
}
};

TEST(TemplateTest, TemplateSpecializationTest)
{
C c;
int i = 2;
c.foo(i);

A a;
c.foo(a);
}
image

 

Works as I expect. Conversions are applied to argument when calls foo() function.

 

refer to C++ Primer.

 

Update

If we use template specializations and overloaded functions at the same time. The VC++(VS2005) complier will pick up the overloaded function.

class A
{
};

class C
{
public:
template<typename T>
void foo(T t)
{
printf("void foo(T t)\n");
}

template<>
void foo(const int i)
{
printf("void foo(int i)\n");
}

template<>
void foo(A a)
{
printf("template<> void foo(A a)\n");
}

void foo(A a);
};

TEST(TemplateTest, TemplateSpecializationTest)
{
C c;
int i = 2;
c.foo(i);

A a;
c.foo(a);
}

void C::foo(A a)
{
printf("void foo(A a)\n");
}
image

But I don’t recommend to use both function templates and nontemplate functions at the same time. Because it will surprise the users to use it.




    本文转自Jake Lin博客园博客,原文链接:http://www.cnblogs.com/procoder/archive/2010/06/28/Template_Specializations_vs_function_overloading.html,如需转载请自行联系原作者


相关文章
|
JavaScript
Vue 报错Failed to mount component: template or render function not defined
Vue 报错Failed to mount component: template or render function not defined
Vue 报错Failed to mount component: template or render function not defined
|
JavaScript
uniapp:[Vue warn]: Failed to mount component: template or render function not de
uniapp:[Vue warn]: Failed to mount component: template or render function not de
328 0
uniapp:[Vue warn]: Failed to mount component: template or render function not de
|
JavaScript
uniapp:[Vue warn]: Failed to mount component: template or render function not defined. found in
uniapp:[Vue warn]: Failed to mount component: template or render function not defined. found in
1003 0
uniapp:[Vue warn]: Failed to mount component: template or render function not defined. found in
|
6月前
|
人工智能 Python
083_类_对象_成员方法_method_函数_function_isinstance
本内容主要讲解Python中的数据类型与面向对象基础。回顾了变量类型(如字符串`str`和整型`int`)及其相互转换,探讨了加法在不同类型中的表现。通过超市商品分类比喻,引出“类型”概念,并深入解析类(class)与对象(object)的关系,例如具体橘子是橘子类的实例。还介绍了`isinstance`函数判断类型、`type`与`help`探索类型属性,以及`str`和`int`的不同方法。最终总结类是抽象类型,对象是其实例,不同类型的对象有独特运算和方法,为后续学习埋下伏笔。
112 7
083_类_对象_成员方法_method_函数_function_isinstance

热门文章

最新文章