小编Woj*_*ilo的帖子

使用libstdc ++在string,u16string和u32string之间进行转换

我发现这个主题在string,u16string和u32string之间转换,解决方案(看起来非常棒)只能使用libc ++而不是libstdc ++.目前libc ++不可用 - 在Windows上很难编译并且不能正常工作.

有没有使用C++ 11和libstdc ++在这些表示之间进行转换的方法,它适用于所有平台?

我特别感兴趣的是将u32_string转换为字符串(utf8),反之亦然.

c++ string unicode unicode-string c++11

5
推荐指数
1
解决办法
1587
查看次数

Python < - > C++面向对象的通信

我想用C++编写一个守护进程,它将保存图形数据结构并计算一些依赖性.我还希望拥有Python Batch(也是守护进程 - 基于HTML的GUI的后端),它将允许用户在这些C++结构上进行交互操作 - 添加/删除/连接/ ...节点并读取计算结果.

我很乐意选择最好的沟通机制.

强制功能是:

  1. Python和C++应该能够以面向对象的方式在节点上运行,所以我希望能够编写类似的代码 n1 = node('a'); n2 = n1.add_subnode('b'); n2.ports('test').connect(node('c'))
  2. Python Batch不必与C++守护进程"分离" - 它们可以具有相同的生命周期(但是如果C++崩溃或出现错误,以某种方式将批处理从C++守护进程中分离出来会很好 - 这种分离是可选的)
  3. 通信应该很快 - Python应该能够获得有关许多节点的信息,并允许最终用户尽可能顺利地工作.

目前我在考虑:

  1. IPC(如0MQ)具有某种数据序列化机制.
  2. RPC基于Protocol BuffersThrift.
  3. 基于Boost.Python的集成

IPC和RPC解决方案似乎很好,但我必须编写一个大包装器来从第1点获取功能.另一方面,我没有找到有关在C++守护程序中使用Boost.Python的信息,我不知道它是否是甚至可能.

c++ python rpc boost ipc

5
推荐指数
1
解决办法
1312
查看次数

输入实例函数的推断

问题

我希望能够创建2 data types:AB创建2个函数f:

  • f :: A -> Int -> Int
  • f :: B -> String -> String -> String

我能做到的唯一方法(就我所知)是使用type classesinstances.

问题是,我不想明确写f签名 - 我希望类型检查器为我推断它.可能吗?

示例代码

{-# LANGUAGE FlexibleInstances, FunctionalDependencies, UndecidableInstances #-}

data A = A{ax::Int} deriving(Show)
data B = B{bx::Int} deriving(Show)
data C = C{cx::Int} deriving(Show)

-- I don't want to explicit say the signature is Int->Int
-- I would love to write: 
-- instance Func_f …
Run Code Online (Sandbox Code Playgroud)

haskell types type-inference ghc

4
推荐指数
1
解决办法
219
查看次数

基于位置的元组模式匹配在Haskell中

是否可以在Haskell中模式匹配元组,但不知道元组的维数?我想创建一个与任何元组相对应的函数,第一个元素是A:

data A = A Int
test args@(A a,..) = a
Run Code Online (Sandbox Code Playgroud)

我知道有Data.Tuple.Select模块,我可以像这样使用它:

test args = case sel1 args of
    A a -> a
    ...
Run Code Online (Sandbox Code Playgroud)

但这是否是这样做的唯一方法,或者Haskell有一些默认机制来匹配任何维度元组?

haskell tuples pattern-matching

4
推荐指数
1
解决办法
365
查看次数

如何将boost :: string_ref与std :: string进行比较

我正在尝试boost::string_ref按照我的意愿工作,但我现在面临一个问题 - 代码无法编译:

 #include <boost/utility/string_ref.hpp> 
 #include <iostream> 
 #include <string> 

 using namespace std;

int main() {
   string test = "test";
   boost::string_ref rtest(test);
   cout << (rtest == "test")<<endl;
}
Run Code Online (Sandbox Code Playgroud)

并且gcc会抛出30kB的错误日志

source.cpp: In function 'int main()':
source.cpp:10:19: error: no match for 'operator==' (operand types are 'boost::string_ref {aka boost::basic_string_ref<char, std::char_traits<char> >}' and 'const char [5]')
    cout << (rtest == "test")<<endl;
                   ^
Run Code Online (Sandbox Code Playgroud)

如何比较boost::string_refstd::string

c++ string boost stl

3
推荐指数
1
解决办法
4685
查看次数

切换不能在C++中工作

我今天遇到了一个非常奇怪的问题.让concider得到以下代码:

int llex(){
    cout<<"enter 1"<<endl;
    char32_t c = U'(';
    cout<<(c==U'#')<<endl;
    switch(c){
    case U'#':
        cout<<"enter 2"<<endl;
        return 5;
    default:
        break;
    }
}

int main( int argc, char** argv)
{
    cout<<"enter 1"<<endl;
    char32_t c = U'(';
    cout<<(c==U'#')<<endl;
    switch(c){
    case U'#':
        cout<<"enter 2"<<endl;
        return 5;
    default:
        break;
    }

    cout << "------------" << endl;
    llex();
}
Run Code Online (Sandbox Code Playgroud)

输出是:

enter 1
0
------------
enter 1
0
enter 2
Run Code Online (Sandbox Code Playgroud)

请注意,main中的代码对于llex函数中的代码是IDENTICAL .他们为什么输出不同的结果 (我在clang上使用C++ 11).

c++ unicode string-literals switch-statement c++11

3
推荐指数
1
解决办法
349
查看次数

Haskell中两个不相关类型之间的大小写

是否可以case在Haskell中的两个不相关类型之间使用表达式,就像在此示例(不工作)代码中一样:

data A = A
data B = B

f x = case x of
  A -> 1
  B -> 2

main = do
  print $ test A
  return ()
Run Code Online (Sandbox Code Playgroud)

我知道我可以Either在这里使用,但是这段代码并不打算使用 - 我想深入学习Haskell类型系统并看看可以做些什么.

haskell types type-systems

3
推荐指数
1
解决办法
117
查看次数

Haskell中不完整的隐式类型推断

让我们考虑以下示例:

data A = A{x::Int} deriving(Show)

instance Func_f (A -> String) where
    f _ = "ala"

class Func_f a where
    f :: a

main :: IO ()
main = do 
    let 
        a = A 5
        x = f a 
    print 5
Run Code Online (Sandbox Code Playgroud)

用.编译 ghc -XFlexibleInstances main.hs

(我试过了-XExtendedDefaultRules,但没有任何进展)

为什么在编译时会出现错误?:

main.hs:25:21:
    No instance for (Func_f (A -> t0)) arising from a use of `f'
    The type variable `t0' is ambiguous
    Possible fix: add a type signature that fixes these type variable(s) …
Run Code Online (Sandbox Code Playgroud)

haskell types overloading type-inference typeclass

2
推荐指数
1
解决办法
388
查看次数

Ruby方法参数?

我是Ruby的新手,我完全厌恶.为什么以下代码:

def some_method(v=1) 10*v end

puts (some_method (1).next)
puts  some_method (1).next
Run Code Online (Sandbox Code Playgroud)

评估:

20
11
Run Code Online (Sandbox Code Playgroud)

ruby syntax

2
推荐指数
1
解决办法
177
查看次数