假设我有以下代码:
class some_class{};
some_class some_function()
{
return some_class();
}
Run Code Online (Sandbox Code Playgroud)
这似乎工作得很好,并省去了为了生成返回值而必须声明变量的麻烦.但我认为我从未在任何教程或参考中看到过这一点.这是编译器特定的东西(visual C++)吗?或者这是做错了什么?
似乎以下调用执行您期望的操作(关闭流并且不允许任何进一步的输入 - 在流上等待输入的任何内容都会返回错误),但它是否保证在所有编译器/平台上都是正确的?
close(fileno(stdin));
fclose(stdin);
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用一个可能抛出多个异常的仿函数F(在下面的示例中,Checked和SQLException).我希望能够使用F作为参数调用一个函数,这样任何检查的异常F抛出(除了将在内部处理的SQLException)都会被重新抛出.
import java.sql.Connection;
import java.sql.SQLException;
class Checked extends Exception {
public Checked() {
super();
}
}
@FunctionalInterface
interface SQLExceptionThrowingFunction<T, U, E extends Exception> {
U apply(T t) throws E, SQLException;
}
class ConnectionPool {
public static <T, E extends Exception> T call(Class<E> exceptionClass, SQLExceptionThrowingFunction<Connection, T, E> f) throws E {
throw new UnsupportedOperationException("unimportant");
}
}
class Test {
static Void mayThrow0(Connection c) {
throw new UnsupportedOperationException("unimportant");
}
static <E extends Exception> Void mayThrow1(Connection c) throws E {
throw new UnsupportedOperationException("unimportant"); …Run Code Online (Sandbox Code Playgroud) 我在交流代码中有这些标题
#include <stdio.h>
#include <unistd.h>
Run Code Online (Sandbox Code Playgroud)
一切都编译得很好,直到我将-std = c99标志添加到gcc命令(启用限制).这引发了以下错误.
警告:隐式声明函数
fileno错误:
F_LOCK未声明(在此函数中首次使用)
错误:(每个未声明的标识符仅报告一次错误:对于它出现的每个函数.)
错误:F_ULOCK未声明(在此函数中首次使用)
任何解决这些错误/警告的想法?
假设我有一个整数排序集xs,我想检索xs中所有[x,y)的整数,即.在x和y之间.
我可以:
(select #(and (>= % x) (< % y)) xs)
Run Code Online (Sandbox Code Playgroud)
但这是低效的 - O(n)当它可能是O(log n)时,我期望返回的元素数量很少.使用take-while和drop-while会让我在到达y后退出,但我仍然无法有效地跳转到x.
我只是在学习clojure所以这里是我将如何在C++中做到这一点:
set<int>::iterator first = xs.lower_bound(x);
set<int>::iterator last = xs.lower_bound(y);
for (; first != last; ++first)
// do something with *first
Run Code Online (Sandbox Code Playgroud)
我可以在clojure中这样做吗?
说我有以下课程:
template<class T>
struct A
{
static int value;
};
template<class T>
int A<T>::value = 0;
Run Code Online (Sandbox Code Playgroud)
我可以专注A::value于一个没有问题的具体类型:
struct B
{
};
template<>
int A<B>::value = 1;
Run Code Online (Sandbox Code Playgroud)
我想在模板类型上专门化A ::值,我尝试了以下方法:
template<class T>
struct C
{
};
// error: template definition of non-template 'int A<C<T> >::value'
template<>
template<class T>
int A<C<T> >::value = 2;
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点,或者只能在非模板类型上专门化A ::值?
有没有办法从finally子句中检测到异常是在抛出的过程中?
请参阅以下示例:
try {
// code that may or may not throw an exception
} finally {
SomeCleanupFunctionThatThrows();
// if currently executing an exception, exit the program,
// otherwise just let the exception thrown by the function
// above propagate
}
Run Code Online (Sandbox Code Playgroud)
或者忽略了一个例外,你唯一可以做的事情是什么?
在C++中,它甚至不允许您忽略其中一个异常,只调用terminate().大多数其他语言使用与java相同的规则.
据我所知,所有GUI工具包基本上都是相同的。
据我所知,大多数与每个单独工具箱的语言绑定或多或少都是API的字面翻译。在我看来,这似乎使任何编程语言都和其他语言一样具有生产力。
一些工具箱不仅将自己标为GUI工具箱,而且还标为“应用程序框架”,例如wxWidgets。它们为网络,数据结构,日志记录,线程和数据库访问等其他内容添加了API。考虑到其他大多数东西通常都具有更好的库来访问所需的功能,因此似乎在决定工具包之间并不是特别重要。实际上,如果您已经了解了这些内容,那么选择一个简单的工具箱并且知道它只是一个GUI工具箱(如GTK +或FLTK)将是有益的。
是否有与该模型完全不同的GUI库?
当有人尝试进行GUI编程时,您会建议如何在GUI工具箱之间进行选择-甚至哪个工具箱真的重要吗?哪种编程语言通常最容易开发GUI应用程序-还是应该坚持我所知道的?
有没有一种优雅的方法来基于其模板参数之一专门化模板?
IE浏览器.
template<int N> struct Junk {
static int foo() {
// stuff
return Junk<N - 1>::foo();
}
};
// compile error: template argument '(size * 5)' involves template parameter(s)
template<int N> struct Junk<N*5> {
static int foo() {
// stuff
return N;
}
};
template<> struct Junk<0> {
static int foo() {
// stuff
return 0;
}
};
Run Code Online (Sandbox Code Playgroud)
IE浏览器.我试图专门化一个基于参数可被5整除的模板.我似乎可以这样做的唯一方法如下:
template<int N> struct JunkDivisibleBy5 {
static int foo() {
// stuff
return N;
}
};
template<int N> struct Junk { …Run Code Online (Sandbox Code Playgroud) 例如,对于操作容器映射的两个线程,测试迭代器是否仍然有效的正确方法是什么(出于性能原因)?
或者只是间接的方式可以做到这一点.这个示例代码:
#define _SECURE_SCL 1
//http://msdn2.microsoft.com/en-us/library/aa985973.aspx
#define _SECURE_SCL_THROWS 1
#include "map"
#include "string"
#include "exception"
#include "iostream"
using namespace std;
void main(void)
{
map<string, string> map_test;
map<string, string>::iterator iter_map_test;
map_test [ "AAAAA" ] = "11111";
map_test [ "BBBBB" ] = "22222";
map_test [ "CCCCC" ] = "33333";
iter_map_test = map_test.find ("BBBBB");
map_test.erase ("BBBBB");
try
{
string value = (*iter_map_test).second;
}
catch ( exception & e )
{
cout << e.what() << endl;
}
catch ( ... )
{
cout << …Run Code Online (Sandbox Code Playgroud) 我需要一个函数count_permutations()来返回给定范围的排列数.假设允许修改范围,并从第一个排列开始,我可以天真地实现这个,因为重复调用next_permutation(),如下所示:
template<class Ret, class Iter>
Ret count_permutations(Iter first, Iter last)
{
Ret ret = 0;
do {
++ret;
} while (next_permutation(first, last));
return ret;
}
Run Code Online (Sandbox Code Playgroud)
是否有更快的方法不需要遍历所有排列来找到答案?它仍然可以假设输入可以被修改,并在第一个排列中开始,但显然如果可以在没有这些假设的情况下实现它也会很棒.