[英]Explicit void pointer as function parameter
我有一个功能:
int foo(void * ptr)
{
// ...
}
我可以在C ++ 11/14中语法上(不使用编译器警告等)禁用传递void *
本身以外的指针吗?
例如,现在它可以被称为:
foo(new int(42));
我需要禁用它。
我想还有很多其他方法可以做到这一点。
使用模板函数很简单(它也适用于C ++ 98)
template <typename X>
int foo (X * ptr);
int foo (void * ptr)
{ return 1; }
int main()
{
int i;
void * vp = &i;
foo(vp); // OK
foo(&i); // linker error
return 0;
}
正如frymode所指出的,前面的解决方案给出了链接器错误,而不是编译器错误,并且最好得到编译器错误。
使用delete
(来自C ++ 11)我们可以通过使用它来获得编译器错误:
template <typename X>
int foo (X ptr) = delete;
希望这可以帮助。
如果你想要精确的类型匹配,你可以使用std :: enable_if和std :: is_same
#include <iostream>
#include <type_traits>
template <typename T,
typename = typename std::enable_if_t<std::is_same<T, void*>::value>>
int foo(T value)
{
return 5;
}
int main()
{
// return foo(new int(42)); // error: no matching function for call to 'foo(int*)'
return foo((void*)(new int(42)));
}
您可以将该函数转换为模板,然后使用type_traits
的static_assert
和std::is_void
:
template<typename T>
int foo(T *ptr) {
static_assert(std::is_void<T>::value, "!");
// ....
}
否则,您可以在返回类型上使用std::enable_if_t
:
template<typename T>
std::enable_if_t<std::is_void<T>::value, int>
foo(T *ptr) {
// ....
return 0;
}
等等,其他用户已经提出了其他有趣的解决方案。
这是一个最小的工作示例:
#include<type_traits>
template<typename T>
int foo(T *ptr) {
static_assert(std::is_void<T>::value, "!");
// ....
return 0;
}
int main() {
int i = 42;
void *p = &i;
foo(p);
// foo(&i); // compile error
}
惯用的方法是创建一个新类型来表示void*
以避免您描述的问题。 许多优秀的C ++实践倡导者建议创建类型以避免对应该传递什么产生任何疑问,并避免编译器让你这么做。
class MyVoid
{
//... implement in a way that makes your life easy to do whatever you are trying to do with your void* stuff
};
int foo(MyVoid ptr)
{
// ...
}
template<class> struct check_void;
template<> struct check_void<void> { typedef void type; };
template<class T> typename check_void<T>::type *foo(T *ptr) { return ptr; }
int main()
{
foo(static_cast<void *>(0)); // success
foo(static_cast<int *>(0)); // failure
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.