要检查是否可以将课程打入另一个
To Check if a class can be typecast to another
如果我有模板函数
template<class T, class U>
T foo (U a);
我如何检查类U
的对象是否可以键入将其施放到对象T
那就是类U
具有成员函数
operator T(); // Whatever T maybe
或类T
具有构造函数
T(U& a); //ie constructs object with the help of the variable of type U
您可以使用std :: is_convertible(因为C 11):
template<class T, class U>
T foo (U a) {
if (std::is_convertible_v<U, T>) { /*...*/ }
// ...
}
请注意,添加了is_convertible_v
,因为C 17,如果您的编译器仍然不支持它,则可以使用std::is_convertible<U, T>::value
。
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在另一个线程中调用luaL_error会引发qWarning
- 最佳做法是从另一个线程访问 qml 中的Q_PROPERTY
- C++线程:如何在一个线程仍在运行时阻止另一个线程执行 (Win32)
- 结束另一个线程中使用的对象的生存期
- 将类成员函数作为线程调用到另一个类成员函数时发出警告消息
- 使用线程从另一个成员函数调用一个Member函数
- 提升线程问题,当一个线程与另一个线程没有相同的副本时如何共享变量?
- 如果不同的线程调用另一个不同线程的相同信号,则需要静音
- 要检查是否可以将课程打入另一个
- 当一个线程在另一个线程中捕获异常后运行时,进程会使用 SIGABRT 中止
- 可以终止线程从另一个进程终止线程
- C++/Qt-从一个线程到另一个线程槽的信号
- 如何让每个线程传递另一个参数
- 等待线程/s直到另一个线程/s结束
- 让一个线程检查另一个线程的状态
- Windows线程从另一个线程休眠和唤醒
- 线程设计和设计,并在 Win32 中从一个线程调用另一个线程C++函数
- C++,如何使用另一个类的线程更改另一个类的变量
- 如何在C++中调用一个线程"join()"另一个函数的方法?