我习惯于构建我的C++项目,如下所示:
int main(int ac, char* av[])
{
try
{
Object foo;
foo.run()
/* ... */
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
return 1;
}
catch (...)
{
std::cerr << "Unknown error." << std::endl;
return 1;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想知道这是一个好习惯,还是在"预期"产生错误的小块代码上使用try/catch块更好?
我想让以下内容运行
def myfunction(a, b) do
IO.puts "Success"
end
def runstuff do
jobs = {&myfunction/2, [1, 3]}
{to_run, args} = jobs
to_run.(args) # broken code
end
Run Code Online (Sandbox Code Playgroud)
好吧,上面的代码被打破,但我认为显示我想要实现的,我是一个快乐的Elixir新手(显然:))我希望它可以用一些elixir宏魔法解决.编辑:根据评论移动作业.
我正在尝试实现以下比较函数模板:
template<typename T>
int compare(T x, T y)
{
if (x > y)
return 1;
else if (x < y)
return -1;
else
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它适用于所有经典类型,但以下类不起作用:
class c
{
private:
c &operator=(const c&) {return *this;}
c(const c &){}
public:
bool operator==(const c&) const {return true;}
bool operator>(const c&) const {return false;}
bool operator<(const c&) const {return false;}
c(){}
};
Run Code Online (Sandbox Code Playgroud)
当我尝试比较我的类的两个实例时,编译器大喊他不能,因为复制ctor是私有的,我试图将引用传递给我的函数模板,但它不起作用.有任何想法吗 ?
我正在尝试实现以下功能:
void dump() const
{
size_t it = 0;
std::cout << "[";
while (it < this->_size);
{
std::cout << (this->_arr)[it];
if ((this->_arr)[it + 1])
std::cout << ", ";
it++;
}
std::cout << "]" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这似乎会导致一个无限循环,我猜这是因为使用不当std::cout
,而且std::endl
,我可以看到如何管理它.有任何想法吗?