如何检测明确的铸造运算符,应IS_Constructibles工作
How to detect explicit cast operator, should is_constructible work?
我想检测,如果类具有显式转换操作员。我已经尝试使用IS_Construction,但是MSVC 19.00.23506。
,以下断言失败了。#include <string>
#include <type_traits>
struct Foo { explicit operator std::string() { return ""; } };
static_assert(std::is_constructible<std::string, Foo>::value, "Fail");
我的问题是:
- 应该在这里IS_Construction工作吗?
- 如何以不同的方式检测它?
应该在这里is_constructible工作吗?
我认为应该这样做,因为没有什么可以排除明确的转换。G 4.8 (及以上)和clang 3.6 (及以上)成功编译了您的代码。
如何以不同的方式检测它?
您可以尝试使用检测成语,该是针对C 17的标准化,但可以在C 11 中实现。(在CPPReference页面上提供了C 11兼式实现。)
struct Foo { explicit operator std::string() { return ""; } };
template <class T>
using convertible_to_string = decltype(std::string{std::declval<T>()});
// Passes!
static_assert(std::experimental::is_detected<convertible_to_string, Foo>::value, "");
wandbox示例
(!)注意:此方法似乎在MSVC 19.10 上无法正常工作(在此处测试)。这是我使用的完整片段。
相关文章:
- 为什么比较运算符如此快速
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 增量运算符与后缀混淆
- 一个关于在C++中重载布尔运算符的问题
- 运算符C++ "delete []"仅删除 2 个前值
- 模板类无法识别友元运算符
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 关闭||运算符优化
- 通过继承类使用来自不同命名空间的运算符
- C++Cast运算符过载
- 如何使用AngelScript注册SFML Vector2运算符
- 重载元组索引运算符-C++
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 在析构函数中调用"delete"运算符时"compiler is out of heap space"编译器错误
- 为什么我"error: expression is not assignable"组合增量运算符
- C++运算符重载错误:与"is >> "(""中的"运算符>>不匹配
- 为基元类型重载了Is+运算符