小编Sha*_*iar的帖子

在单例中使用已删除的复制构造函数

我实现了像Singleton模式这个,还有我的代码:

头文件:

class Settings_manager{
public:
    static Settings_manager& get_instance();

    void operator=(Settings_manager const&) =delete;
    Settings_manager(Settings_manager const&) =delete;
...

private:
    Settings_manager();
};
Run Code Online (Sandbox Code Playgroud)

执行:

Settings_manager& Settings_manager::get_instance()
{
    static Settings_manager instance;
    return instance;
}

Settings_manager::Settings_manager()
{
    read_file();
}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用这样的get_instance函数main:

Settings_manager set = Settings_manager::get_instance();
Run Code Online (Sandbox Code Playgroud)

要么 Settings_manager set = std::move(Settings_manager::get_instance());

我明白了

error: use of deleted function 'Settings_manager::Settings_manager(const Settings_manager&)'
 Settings_manager set = Settings_manager::get_instance();
Run Code Online (Sandbox Code Playgroud)

有人能说出来,有什么不对并解释一下吗?谢谢.

c++ singleton c++11 c++14

4
推荐指数
2
解决办法
1305
查看次数

SomeException的句柄被忽略

我有一个普通的haskell代码,它读取文件并处理任何异常:

handle ((\_->return "ERROR") :: SomeException -> IO String) $ readFile "file.txt"
Run Code Online (Sandbox Code Playgroud)

当我尝试读取错误的编码文件时,总是出现错误:

*** Exception: file.txt: hGetContents: invalid argument (invalid byte sequence)
Run Code Online (Sandbox Code Playgroud)

并且程序不会进入我的异常处理函数。我也尝试使用IOErrorIOException类型代替,SomeException但是它什么也没改变。

如果我使用句柄打开类似文件并使用代码读取它:

*** Exception: file.txt: hGetContents: invalid argument (invalid byte sequence)
Run Code Online (Sandbox Code Playgroud)

工作正常。

如何捕获hGetContentsreadFile正确方式传递的异常?

haskell functional-programming exception

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

即使使用 bang 模式也无法调用 IO() 函数

我有一些库,它具有使用 gnuplot 库进行绘图的功能:

import Graphics.Gnuplot.Simple

drawMap :: [(Int, Int)] -> IO ()                                                   
drawMap m = do                                                                     
  !a <- plotList [] m                                                              
  print a 
Run Code Online (Sandbox Code Playgroud)

我像这样从 main 调用这个函数:

main = do                                                                          
  !a <- drawMap [(1,2),(3,5)]                                                      
  print a     
Run Code Online (Sandbox Code Playgroud)

我用堆栈构建项目,并尝试了 -O2 和 -O0 优化,但 plot 永远print a不起作用(函数总是被成功调用并打印())。我怎样才能强制绘图以及为什么它不工作,使用库,但如果我只是plotList从 main调用就可以工作?

更新。

在 main 和drawMapby$!中使用严格的应用程序也不起作用:

drawMap :: [(Int, Int)] -> IO ()                                                   
drawMap m = plotList [] $! m                                                                  

main = do                                                                                                                                                 
  drawMap $! [(1,2),(3,5)] 
Run Code Online (Sandbox Code Playgroud)

UPD 2 …

haskell lazy-evaluation

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

不能构造无限类型:e〜[e]

为什么这段代码:

a=array ((0,0),(5,5)) [((i,j),x) | i <- [0..5], j <- [0..5], x <- a!(i,j)]
Run Code Online (Sandbox Code Playgroud)

导致错误cannot construct the infinite type: e ~ [e],但如果像这样重写:

a=array ((0,0),(5,5)) [((i,j),a!(i,j)) | i <- [0..5], j <- [0..5]]
Run Code Online (Sandbox Code Playgroud)

它工作正常吗?

haskell

0
推荐指数
1
解决办法
60
查看次数

动态解析字段名称的记录更新

我有一个记录定义如下:

data MyData = MyData
    { name :: String
    , addr :: String
     ... a lot of other fields of String type
    }
Run Code Online (Sandbox Code Playgroud)

接下来我想创建对列表(String, fieldName),如下所示:

fields =
  [ ("NAME", name)
  , ("ADDRESS", addr)
  , ... and for other fields
  ]
Run Code Online (Sandbox Code Playgroud)

最后,我需要一个函数,它可以获取类型的空记录MyData并逐个字段动态填充它,如下所示:

initByStrings strs = foldl (\ d (x, y) -> d{y=(findIn x strs)}) emptyMyData fields 
Run Code Online (Sandbox Code Playgroud)

在 Haskell 中,如果没有像下面这样的长单调结构,这样的行为是否可能?

...
lst = map (\ x -> findIn x strs) fields
f lst where
    f (name:addr:...) = MyData …
Run Code Online (Sandbox Code Playgroud)

haskell functional-programming generic-programming

0
推荐指数
1
解决办法
134
查看次数