这是确保不会发生隐式类型转换的合法方法吗
Is this a legitimate method for making sure implicit type conversion does not happen
这是确保不发生隐式类型转换的合法方法吗?
#include <string>
#include <iostream>
void func(std::string s)
{
std::cout << "Thanks for the stringn";
}
template<class T>
void func(T)=delete;
int main()
{
func("test1");
// str.cc: In function ‘int main()’:
// str.cc:13:16: error: use of deleted function ‘void func(T) [with T = const char*]’
// func("test1");
// ^
// str.cc:9:6: error: declared here
// void func(T)=delete;
// ^
//
func(std::string("test2"));
return 0;
}
我觉得不错。
它对答案也有同样的作用。
是的,该方法确保不允许隐式转换。然而,这也意味着这个性质并不是单独由void func(string)
的定义产生的。因此,为了向读者澄清这一点,您可以使其更加独立,如下所示:
template <typename T, typename U> using RequireExplicit = enable_if_t<is_same<T, U>>;
template <typename T, typename = RequireExplicit<T, string>>
void func(T){}
相关文章:
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 将复杂的非基元C++数据类型转换为 Erlang/Elixir 格式,以使用 NIF 导出方法
- 通过构造函数方法输出的类到类类型转换是 5500 为什么不是 5555
- 在C++中将算术类型转换为 std::array 的最简单方法<uint8_t>
- 从"wcslen"替换到"strnlen_s"时,用"const char*"进行类型转换是正确的方法吗?
- 模拟返回具有多个用户定义转换的类型的方法
- 删除类型转换指针的最佳方法
- 操作方法:将 boost::endian 缓冲区类型转换回本机格式
- 使用正确的数据类型转换java中的c++方法逻辑
- 在 C++ 中修复类型转换警告的最佳方法
- 如何将方法指针类型转换为函数指针类型
- 在C++中实现相关类型之间的自动类型转换的最佳方法是什么?
- 这是确保不会发生隐式类型转换的合法方法吗
- 处理模棱两可的类型转换/转换的正确方法是什么?
- 在Xcode中,将unsigned long类型向下转换为需要c++ int类型的方法
- 正确的转换指针类型的方法
- c++中这两种类型转换方法的区别是什么?
- 在模板参数的方法中添加类型转换时出现clang错误
- 在 C++ 中定义类型转换运算符模板的正确方法是什么?