强制函数仅使用特定类型调用 [英] Force function to be called only with specific types

查看:149
本文介绍了强制函数仅使用特定类型调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在观看投放时强制类型安全char * to bool in C ++ 11 并且建议如果你执行

  template< typename T> 
void foo(T)= delete;

void foo(int f){}

c> foo 只有在给定一个显式的 int 参数时才会工作。我做了一个测试用例:

  template< typename T& 
void foo(T)= delete;

void foo(int f){}

int main()
{
foo(1);
foo(3.0);
foo(short(5));
foo(float(7.0));
foo(long(9));
}


$ b $ p我使用coliru来编译代码 g ++ - std = c ++ 14 -O2 -Wall -pedantic -pthread main.cpp&& ./a.out 实例),我得到了以下错误:

  main.cpp:在函数'int main()':
main.cpp:9 :12:错误:使用删除的函数'void foo(T)[with T = double]'
foo(3.0);
^
main.cpp:2:6:注意:这里声明了
void foo(T)= delete;
^
main.cpp:10:17:错误:使用删除的函数'void foo(T)[with T = short int]'
foo(short(5));
^
main.cpp:2:6:注意:这里声明了
void foo(T)= delete;
^
main.cpp:11:19:error:使用删除的函数'void foo(T)[with T = float]'
foo(float(7.0));
^
main.cpp:2:6:注意:这里声明了
void foo(T)= delete;
^
main.cpp:12:16:错误:使用删除的函数'void foo(T)[with T = long int]'
foo(long(9));
^
main.cpp:2:6:注意:这里声明了
void foo(T)= delete;
^

使用clang编译也会产生类似的错误 p>

现在,当我在 = delete w / cpp / language / function#Deleted_functionsrel =nofollow> cppreference 它表示


如果函数重载


因此,如果cppreference是正确的,那么程序只会出现错误。和我的程序是不成形的这只是意味着它不会编译或是未指定或未定义的行为?

解决方案

是不合格的。首先,对于 foo 的每次调用,我们执行重载分辨率。这将调用:

  foo(1); // foo(int)
foo(3.0); // foo< T> ;, T = double
foo(short(5)); // foo< T> ;, T = short
foo(float(7.0)); // foo< T> ;, T = float
foo(long(9)); // foo< T> ;, T = long

其中四个函数是显式的删除和从[dcl.fct.def.delete]:



不是未定义或未指定的行为。它应该只是不编译。


I was looking at enforcing type safety when casting char* to bool in C++11 and it was suggested that if you do

template<typename T>
void foo(T) = delete;

void foo(int f) {}

That foo will only work when given a explicit int argument. I made a test case:

template<typename T>
void foo(T) = delete;

void foo(int f) {}

int main()
{
    foo(1);
    foo(3.0);
    foo(short(5));
    foo(float(7.0));
    foo(long(9));
}

I used coliru to compile the code with g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out(live example) and I got the following errors:

main.cpp: In function 'int main()':
main.cpp:9:12: error: use of deleted function 'void foo(T) [with T = double]'
     foo(3.0);
            ^
main.cpp:2:6: note: declared here
 void foo(T) = delete;
      ^
main.cpp:10:17: error: use of deleted function 'void foo(T) [with T = short int]'
     foo(short(5));
                 ^
main.cpp:2:6: note: declared here
 void foo(T) = delete;
      ^
main.cpp:11:19: error: use of deleted function 'void foo(T) [with T = float]'
     foo(float(7.0));
                   ^
main.cpp:2:6: note: declared here
 void foo(T) = delete;
      ^
main.cpp:12:16: error: use of deleted function 'void foo(T) [with T = long int]'
     foo(long(9));
                ^
main.cpp:2:6: note: declared here
 void foo(T) = delete;
      ^

compiling with clang also produced similar errors

Now when I was reading about = delete on cppreference it stated

If the function is overloaded, overload resolution takes place first, and the program is only ill-formed if the deleted function was selected.

So if cppreference is correct and my program is ill-formed does this just mean it will not compile or is it unspecified or undefined behavior?

解决方案

Your program is ill-formed. First, for each invocation of foo, we perform overload resolution. That will call:

foo(1);            // foo(int )
foo(3.0);          // foo<T>, T=double
foo(short(5));     // foo<T>, T=short
foo(float(7.0));   // foo<T>, T=float
foo(long(9));      // foo<T>, T=long

Four of those functions are explicitly deleted and, from [dcl.fct.def.delete]:

A program that refers to a deleted function implicitly or explicitly, other than to declare it, is ill-formed.

It's not undefined or unspecified behavior. It should simply not compile.

这篇关于强制函数仅使用特定类型调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆