c++ - 检查类型是否可以作为 boost::lexical_cast<string> 的参数

标签 c++ templates c++11 boost template-meta-programming

我有以下特征类( IsLexCastable )来检查是否可以通过调用 boost::lexical_cast<string> 将类型转换为字符串.它错误地返回 true对于 vector<int> .

#include <iostream>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

#include <boost/lexical_cast.hpp>

using namespace std;
using namespace boost;

namespace std
{
/// Adding to std since these are going to be part of it in C++14.
template <bool B, typename T = void>
using enable_if_t = typename std::enable_if<B, T>::type;
}

template <typename T, typename = void>
struct IsLexCastable : std::false_type
{
};

template <typename T>
struct IsLexCastable<T, std::enable_if_t<std::is_same<std::string, decltype(boost::lexical_cast<std::string>(std::declval<T>()))>::value> > : std::true_type
{
};

int main()
{
  vector<int> a = {1, 2, 3};
  //  cout << lexical_cast<string>(a) << endl;
  cout << IsLexCastable<decltype(a)>::value << endl;
  return 0;
}

这个程序打印1 ,但是 lexical_cast<string>(a)导致编译错误。什么是正确的实现方式IsLexCastable

(这是用 g++48 -std=c++11boost 1.55.0 编译的。)

最佳答案

您的表达式是不够的,因为 lexical_cast 函数模板接受所有内容并且仅通过内部 static_assert 报告错误。而是测试将对象插入 std::ostream 是否有效:

template <typename T, typename=void>
struct IsLexCastable : std::false_type {};

// Can be extended to consider std::wostream as well for completeness
template <typename T>
struct IsLexCastable<T,
            decltype(void(std::declval<std::ostream&>() << std::declval<T>()))>
  : std::true_type {};

Demo .
the documentation 将该要求称为 OutputStreamable , 以及直接强加到源类型上的。


为什么您的实现不起作用?

decltype 只会导致函数模板的实例化声明。内部静态断言是在 lexical_cast 的定义中触发的,因此不能在 SFINAE 中使用。

[临时安装]/10:

If a function template or a member function template specialization is used in a way that involves overload resolution, a declaration of the specialization is implicitly instantiated (14.8.3).

关于c++ - 检查类型是否可以作为 boost::lexical_cast<string> 的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27709461/

相关文章:

C++ 无法重新填充字符串流

c++ - 在 C++ 中更改对象的动态类型

c++ - 模板中的函数类型无法编译

c++ - 采用模板非类型模板参数的函数模板

c++ - 静态 constexpr 类成员何时需要类外定义?

c++ - C/C++ 中的 BST super 节点生成

c++ - 循环中的随机函数

c++ - 如果实例化,如何使模板化变量专门化在编译时失败?

c++ - 为 SDL 事件过滤创建指向成员函数的非常量指针

c++ - 可以使用 C++11 decltype 为现有函数的函数指针创建 typedef 吗?