Scott Meyers发表了他的下一本书EC++ 11的内容和状态.他写道,书中的一个项目可能是"避免std::enable_if功能签名".
std::enable_if 可以用作函数参数,返回类型或类模板或函数模板参数,以有条件地从重载解析中删除函数或类.
在这个问题中,显示了所有三个解决方案
作为功能参数:
template<typename T>
struct Check1
{
template<typename U = T>
U read(typename std::enable_if<
std::is_same<U, int>::value >::type* = 0) { return 42; }
template<typename U = T>
U read(typename std::enable_if<
std::is_same<U, double>::value >::type* = 0) { return 3.14; }
};
Run Code Online (Sandbox Code Playgroud)
作为模板参数:
template<typename T>
struct Check2
{
template<typename U = T, typename std::enable_if<
std::is_same<U, int>::value, int>::type = 0>
U read() { return 42; }
template<typename U = T, …Run Code Online (Sandbox Code Playgroud) 以下代码编译并运行时,我感到很惊讶(vc2012&gcc4.7.2)
class Foo {
struct Bar { int i; };
public:
Bar Baz() { return Bar(); }
};
int main() {
Foo f;
// Foo::Bar b = f.Baz(); // error
auto b = f.Baz(); // ok
std::cout << b.i;
}
Run Code Online (Sandbox Code Playgroud)
这段代码编译得好吗?为什么这是正确的?为什么我可以auto在私有类型上使用,而我不能使用它的名字(如预期的那样)?
我使用加载一些二进制数据
$http.post(url, data, { responseType: "arraybuffer" }).success(
function (data) { /* */ });
Run Code Online (Sandbox Code Playgroud)
如果出现错误,服务器会响应错误的JSON对象
{ "message" : "something went wrong!" }
Run Code Online (Sandbox Code Playgroud)
有没有办法以不同于成功响应的类型获取错误响应?
$http.post(url, data, { responseType: "arraybuffer" })
.success(function (data) { /* */ })
.error(function (data) { /* how to access data.message ??? */ })
Run Code Online (Sandbox Code Playgroud) 我想知道在dunit中测试异常的最佳做法是什么.我对Delphi中的方法指针不是很熟悉.是否有可能将参数绑定到方法指针,以便可以在没有参数的情况下调用它.目前我总是写一个额外的方法来手动执行"绑定".如果SUT有很多投掷方法,那将会很烦人.
// What i did before i knew abput CheckExcepion
procedure MyTest.MyMethod_BadInput_Throws;
var
res: Boolean;
begin
res := false;
try
sut.MyMethod('this is bad');
except
on e : MyExpectedException do:
res := true;
end;
CheckTrue(res);
end;
// What i do now
procedure MyTest.MyMethodWithBadInput;
begin
sut.MyMethod('this is bad');
end;
procedure MyTest.MyMethod_BadInput_Throws;
begin
CheckException(MyMethodWithBadInput, MyExpectedException);
end;
// this would be nice
procedure MyTest.MyMethod_BadInput_Throws;
begin
CheckException(
BindArguments(sut.MyMethod, 'this is bad'), // <-- how to do this
MyExpectedException);
end;
Run Code Online (Sandbox Code Playgroud) 考虑以下示例,我们解析数据并将结果传递给下一个函数:
Content Parse(const std::string& data);
void Process(Content content);
int main()
{
auto data = ReadData();
Process(Parse(data));
}
Run Code Online (Sandbox Code Playgroud)
现在让我们使用更改代码std::optional来处理失败的解析步骤:
optional<Content> Parse(const std::string& data);
void Process(Content content);
int main()
{
auto data = ReadData();
auto content = Parse(data);
if (content)
Process(move(*content));
}
Run Code Online (Sandbox Code Playgroud)
移动是否有效optional<T>::value()?如果它std::optional可以,它也有效boost::optional吗?
函数的Result变量是否有任何保证的默认值,如0,''或nil?或者结果总是在使用前初始化?
我有一个函数返回一个这样的字符串:
function Foo(): String
begin
while {...} do
Result := Result + 'boingbumtschak';
end;
Run Code Online (Sandbox Code Playgroud)
它运行正常,但现在我得到一些字符串,其中包含之前调用该函数的内容.当我Result := ''在开头添加一个,它没关系.我什么时候应该初始化Result变量,什么时候不需要?(字符串,基元,类实例(零))
我正在处理一项任务,我必须以xlsx格式下载报告.报告文件是从服务器成功生成的,也是在客户端接收的.但它没有打开并产生无效的格式错误.
下面是服务器端的代码.
var output = await reportObj.GetExcelData(rParams);
if (output != null){
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(output.ConentBytes)
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = output.FileName
};
return result;
}
Run Code Online (Sandbox Code Playgroud)
以下是客户端的代码:
var saveData = function (response) {
if (response.status === 200) {
var reportData = response.data;
var b = new Blob([reportData], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
saveAs(b,"ReportFile.xlsx");//this is FileSaver.js function
} else {
console.log(response.statusText);
}
};
$scope.getExcelFile = function(reportName, reportParams) {
reportDataService.getExcelReportData(reportName, reportParams, …Run Code Online (Sandbox Code Playgroud) 我注意到,与进程本身认为使用(使用)的内容相比,PHP进程的内存使用情况top或ps报告之间存在很大差异memory_get_usage.
该进程实际使用了多少内存?
与我的某个应用程序一起运行以下代码时:
echo "Memory usage: " . pretty_bytes(memory_get_usage()) . PHP_EOL;
echo "Peak memory usage: " . pretty_bytes(memory_get_peak_usage()) . PHP_EOL;
echo "'Actual' memory usage: " . pretty_bytes(memory_get_usage(true)) . PHP_EOL;
echo "'Actual' peak memory usage: " . pretty_bytes(memory_get_peak_usage(true)) . PHP_EOL;
$ps_output = exec("ps --pid " . getmypid() . " --no-headers -o rss");
echo "'Memory usage according to ps: " . pretty_bytes(intval($ps_output) * 1000);
Run Code Online (Sandbox Code Playgroud)
随机点的输出是:
Memory usage: 4.77 MB
Peak memory usage: 4.99 MB
'Actual' memory …Run Code Online (Sandbox Code Playgroud) 当我实例化抽象类时,是否有任何编译器选项让编译器给我一个错误而不是一个警告?
Foo = class
procedure Bar; virtual; abstract;
end;
var
f : Foo;
begin
f := Foo.Create; // <-- should give me a compile time error
end;
Run Code Online (Sandbox Code Playgroud) 在他最近的一次演讲中,Herb Sutter建议更喜欢免费的begin(container) end(container)功能模板container.begin().我喜欢它,因为可以为所有不带有begin()/ end()方法的可迭代类型提供这些函数.由于我的大多数域类都具有以域语言进行通信的接口,并且不使用诸如begin/end之类的通用名称,因此我现在可以提供与STL容器兼容的可迭代接口和用于循环的范围,而不会弄乱主类接口.我想知道为我自己的类型提供开始/结束函数的最佳方法是什么.我的第一个想法是以与我一样的方式执行它,swap并在我的类型所在的同一命名空间中编写函数.
namespace My
{
class Book
{
public:
typedef std::vector<Page>::const_iterator PageIterator;
PageIterator FirstPage() const { return begin(pages_); }
PageIterator LastPage() const { return end(pages_); }
private:
std::vector<Page> pages_;
};
Book::PageIterator begin(const Book& b)
{
return b.FirstPage();
}
Book::PageIterator end(const Book& b)
{
return b.LastPage();
}
}
Run Code Online (Sandbox Code Playgroud)
可以在这里依赖ADL,还是应该在std命名空间中?我认为另一种方法是在std命名空间中提供特化(不允许在std中重载,对吧?).特别是关于基于范围的循环查找的最佳方法是什么?
c++ ×4
c++11 ×3
delphi ×3
angularjs ×2
javascript ×2
angular-http ×1
auto ×1
c# ×1
c++14 ×1
delphi-2010 ×1
dunit ×1
enable-if ×1
excel ×1
exception ×1
memory ×1
php ×1
sfinae ×1
std ×1
templates ×1
unit-testing ×1