首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >比较std::wstring类型和ChaiScript中wstring文本的表示形式

比较std::wstring类型和ChaiScript中wstring文本的表示形式
EN

Stack Overflow用户
提问于 2015-06-15 14:40:20
回答 2查看 600关注 0票数 3

我想使用std::wstring类型编写ChaiScript代码,就像下面的c++代码一样。

代码语言:javascript
复制
#include <iostream>

int testfunc(std::wstring s, std::wstring t)
{
    if(s==t)
    {
        std::cout << "1" << std::endl;
    }

    if(s[1]==t[1])
    {
        std::cout << "2" << std::endl;
    }

    if(s==L"aaaa")
    {
        std::cout << "3" << std::endl;
    }

    if(s[1]==L'b')
    {
        std::cout << "4" << std::endl;
    }

    return 5;
}

int main()
{
    std::cout << testfunc(std::wstring(L"abcd"), std::wstring(L"abbb"));

    return 0;
}

D:\TestWork\test_chaiscript>t6
2
4
5

比较std::wstring类型的实例是很好的。

代码语言:javascript
复制
#include <iostream>
#include <chaiscript/chaiscript.hpp>
#include <chaiscript/chaiscript_stdlib.hpp>
#include <chaiscript/dispatchkit/bootstrap_stl.hpp>

int main()
{
    chaiscript::ChaiScript chai(chaiscript::Std_Lib::library());

    chai.add(chaiscript::bootstrap::standard_library::string_type<std::wstring>("wstring"));

    std::cout << chai.eval<std::function<int (std::wstring, std::wstring)> >(
        "fun(s, t){"
        "   if(s==t){"
        "       print(\"1\");"
        "   }"
        "   return 3;"
        "}"
    )(std::wstring(L"abcd"), std::wstring(L"abaa"));

    return 0;
}

D:\TestWork\test_chaiscript>t5
3

比较wchar_t类型的实例不起作用。

是否必须添加比较运算符方法?

代码语言:javascript
复制
        "   if(s[1]==t[1]){"
        "       print(\"2\");"
        "   }"

D:\TestWork\test_chaiscript>t5
terminate called after throwing an instance of 'chaiscript::exception::eval_error'
  what():  Error: "Error with numeric operator calling: =="

将std::wstring类型的实例与string类型的文本进行比较不起作用。我不能输入wstring类型的文本。

可以在ChaiScript中输入wstring类型的文字吗?

代码语言:javascript
复制
        "   if(s==\"aaaa\"){"
        "       print(\"2\");"
        "   }"

D:\TestWork\test_chaiscript>t5
terminate called after throwing an instance of 'chaiscript::exception::eval_error'
  what():  Error: "Can not find appropriate '==' operator." With parameters: (wstring, const string)

将wchar_t类型的实例与wchar_t类型的文本进行比较不起作用。我不能输入wchat_t类型的文字。

是否可以在ChaiScript中输入wchat_t类型的文字?

代码语言:javascript
复制
        "   if(s[1]=='b'){"
        "       print(\"2\");"
        "   }"

D:\TestWork\test_chaiscript>t5
terminate called after throwing an instance of 'chaiscript::exception::eval_error'
  what():  Error: "Error with numeric operator calling: =="
EN

回答 2

Stack Overflow用户

发布于 2015-06-17 09:05:17

是的,您需要告诉ChaiScript一些关于wchar_t的信息,比如==操作符。例如:

代码语言:javascript
复制
chai.add(
  chaiscript::fun<bool (wchar_t, wchar_t)>(
    [](wchar_t lhs, wchar_t rhs) { return lhs == rhs; }
  ), "=="
);
票数 0
EN

Stack Overflow用户

发布于 2015-06-18 17:05:05

我认为ChaiScript需要wchar_t的原生支持。

“调用数值运算符时出错:”异常在chaiscript_eval.hpp中生成。

如果变量的类型是算术,则调用Boxed_Number::do_oper()。

代码语言:javascript
复制
// chaiscript_eval.hpp, line 98
if (t_oper != Operators::invalid && t_lhs.get_type_info().is_arithmetic() && t_rhs.get_type_info().is_arithmetic())
{
  // If it's an arithmetic operation we want to short circuit dispatch
  try{
    return Boxed_Number::do_oper(t_oper, t_lhs, t_rhs);
  } catch (const chaiscript::exception::arithmetic_error &) {
    throw;
  } catch (...) {
    throw exception::eval_error("Error with numeric operator calling: " + t_oper_string);
  }
} else {

它似乎使用std::is_arithmetic来确定是否为算术类型。

代码语言:javascript
复制
// type_info.hpp, 133 line
struct Get_Type_Info
{
    typedef T type;

     static Type_Info get()
    {
      return Type_Info(std::is_const<typename std::remove_pointer<typename std::remove_reference<T>::type>::type>::value, std::is_reference<T>::value, std::is_pointer<T>::value, 
          std::is_void<T>::value,
          std::is_arithmetic<T>::value && !std::is_same<typename std::remove_const<T>::type, bool>::value,
          &typeid(T), 
          &typeid(typename Bare_Type<T>::type));
    }
};

wchat_t是算术类型。

代码语言:javascript
复制
std::cout << std::is_arithmetic<wchar_t>::value << std::endl;
std::cout << std::is_arithmetic<std::string>::value << std::endl;

D:\TestWork\test_chaiscript>t5.exe
1
0

如果类型是算术,则调用Boxed_Value的oper()方法。但是,oper()方法不支持wchar_t类型。

代码语言:javascript
复制
// boxed_number.hpp, 295 line
inline static Boxed_Value oper(Operators::Opers t_oper, const Boxed_Value &t_lhs, const Boxed_Value &t_rhs)
{
  const Type_Info &inp_ = t_lhs.get_type_info();

  if (inp_ == typeid(int)) {
    return oper_rhs<int>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(double)) {
    return oper_rhs<double>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(long double)) {
    return oper_rhs<long double>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(float)) {
    return oper_rhs<float>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(char)) {
    return oper_rhs<char>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(unsigned int)) {
    return oper_rhs<unsigned int>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(long)) {
    return oper_rhs<long>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(unsigned long)) {
    return oper_rhs<unsigned long>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(std::int8_t)) {
    return oper_rhs<std::int8_t>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(std::int16_t)) {
    return oper_rhs<std::int16_t>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(std::int32_t)) {
    return oper_rhs<std::int32_t>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(std::int64_t)) {
    return oper_rhs<std::int64_t>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(std::uint8_t)) {
    return oper_rhs<std::uint8_t>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(std::uint16_t)) {
    return oper_rhs<std::uint16_t>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(std::uint32_t)) {
    return oper_rhs<std::uint32_t>(t_oper, t_lhs, t_rhs);
  } else if (inp_ == typeid(std::uint64_t)) {
    return oper_rhs<std::uint64_t>(t_oper, t_lhs, t_rhs);
  } else  {
    throw chaiscript::detail::exception::bad_any_cast();
  }
}

我添加了代码来支持如下所示的wchar_t类型,在所有地方使用inp字符类型in ( == )。

代码语言:javascript
复制
} else if (inp_ == typeid(char)) {
    return go<LHS, char>(t_oper, t_lhs, t_rhs);
} else if (inp_ == typeid(wchar_t)) {
    return go<LHS, wchar_t>(t_oper, t_lhs, t_rhs);
} else if (inp_ == typeid(unsigned int)) {

我的测试代码运行良好。

代码语言:javascript
复制
std::cout << chai.eval<std::function<int (wchar_t a, wchar_t b)> >(
    "fun(a, b){"
    "   if(a==b){"
    "       print(\"2\");"
    "   }"
    "   return 3;"
    "}"
)(wchar_t(L'a'), wchar_t(L'a'));

D:\TestWork\test_chaiscript>t5.exe
2
3
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30838757

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档