我有一个2D数组,我想定义一个函数,它返回用户使用运算符重载的索引值.换一种说法:
void MyMatrix::ReturnValue()
{
int row = 0, col = 0;
cout << "Return Value From the last Matrix" << endl;
cout << "----------------------------------" << endl;
cout << "Please Enter the index: [" << row << "][" << col << "] =" << ((*this).matrix)[row][col] << endl;
}
Run Code Online (Sandbox Code Playgroud)
该操作((*this).matrix)[row][col]
应该返回一个int
.我不知道如何建立operator [][]
.
或者,我可以连接几个调用operator []
,但我没有成功,因为第一次调用该操作将返回int*
,第二次将返回int
,它强制构建另一个运算符,我不想去做.
我能做什么?谢谢,
我有这个变量:
var stars = this.parentNode.children
Run Code Online (Sandbox Code Playgroud)
它的价值是:
[span.rate, span.rate, span.rate.rated, span.rate.rated, span.rate.rated]
Run Code Online (Sandbox Code Playgroud)
现在我想扭转它,但如果我尝试:
stars.reverse()
Run Code Online (Sandbox Code Playgroud)
我知道了
Uncaught TypeError: stars.reverse is not a functionupdateRateStar @ app.js:75(anonymous function) @ app.js:98
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么它适用于如下数组:
[1,2,3]
Run Code Online (Sandbox Code Playgroud)
所以,如果我尝试:
[1,2,3].reverse()
Run Code Online (Sandbox Code Playgroud)
有用.因此我无法理解这个问题
我有一个a
存储值[0 1 2 3 5]
和其他向量的向量removelist
存储要删除的索引[0 1 2]
,以便[3 5]
最后离开.当我实现以下代码时,它会意外删除项目,因为向量a
将在此过程中更改顺序.我有什么方法可以实现我的目标吗?
for (int i = 0; i<removelist.size() ; i++)
a.erase(a.begin() + removelist[i]);
Run Code Online (Sandbox Code Playgroud) 在这里我发现:
默认情况下,继承构造函数都是noexcept(true),除非它们需要调用noexcept(false)函数,在这种情况下这些函数是noexcept(false).
这是否意味着在下面的示例中,继承的构造函数是noexcept(true)
,即使它已经noexcept(false)
在基类中显式定义,或者它被认为是一个函数是noexcept(false)被调用?
struct Base {
Base() noexcept(false) { }
};
struct Derived: public Base {
using Base::Base;
};
int main() {
Derived d;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Google 服务帐户创建日历活动(在其自己的日历中)并邀请一些与会者。
当我使用服务帐户和 JWT 身份验证创建事件时,事件已成功创建,但受邀者未收到电子邮件通知 - 请参阅下面的代码。如果我使用客户帐户,则会发送电子邮件通知,但我不想诉诸于此。
难道我做错了什么?
var google = require('googleapis');
var SCOPES = ['https://www.googleapis.com/auth/calendar'];
var key = require("./API-Project-key.json");
var jwtClient = new google.auth.JWT(key.client_email, null, key.private_key, SCOPES, null);
jwtClient.authorize(function(err) {
if (err) {
console.log(err);
return;
}
createEvent(jwtClient);
});
/**
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
function createEvent (auth) {
var calendar = google.calendar('v3');
var event = {
'summary': 'Test Event',
'location': 'London, UK',
'description': 'This is a sample event',
'start': {
'dateTime': '2016-03-15T20:00:00',
'timeZone': 'Europe/London', …
Run Code Online (Sandbox Code Playgroud) 考虑以下最小的例子:
struct T { void operator()() { } };
struct S { void operator()(int) { } };
struct U: T, S {
U(): T{}, S{} { }
//using T::operator();
//using S::operator();
};
int main() {
U u;
u(42);
}
Run Code Online (Sandbox Code Playgroud)
它与clang 3.8一样编译.
无论如何,它无法使用GCC 6.1编译并出现错误:
12:错误:成员'operator()'的请求不明确
请注意,如果取消注释指令,GCC 6.1可以正常工作using
.
据我所知,没有理由出现这样的错误,似乎clang是按预期工作的那个.
在打开虫子之前,我不知道是谁,我想问一下我是否遗漏了一些东西.
换句话说,在这种情况下给出错误是正确的行为,或者它本身就是一个错误?
具有观察者模式。
class Observer
{
virtual eventA()=0;
virtual eventB()=0;
...
virtual eventZ()=0;
}
Run Code Online (Sandbox Code Playgroud)
Observer类不能更改,但是我的类仅对事件B感兴趣。因此,我需要:
class MyObserver{
eventA() override {}
eventB() override { /* Do something */ }
eventC() override {}
...
eventZ() override {}
}
Run Code Online (Sandbox Code Playgroud)
清空所有事件的开销,特别是如果您有一个始终要在cpp文件中实现的策略(显然是模板除外)时,这是开销。
C ++ 11是否为此提供任何关键字?喜欢
...
eventC() override = empty;
...
Run Code Online (Sandbox Code Playgroud)
这样,我就不需要在CPP文件中添加空的实现。
从C++ 11到C++ 17,range-for循环等效于以下代码:
{
auto && __range = range_expression ;
for (auto __begin = begin_expr, __end = end_expr; __begin != __end; ++__begin) {
range_declaration = *__begin;
loop_statement
}
}
Run Code Online (Sandbox Code Playgroud)
从C++ 17开始,等价将(显然)更新为这个:
{
auto && __range = range_expression ;
auto __begin = begin_expr ;
auto __end = end_expr ;
for ( ; __begin != __end; ++__begin) {
range_declaration = *__begin;
loop_statement
}
}
Run Code Online (Sandbox Code Playgroud)
乍一看,我能看到的唯一区别在于,__begin
并且__end
不再共享类型,只要它们(让我说)可比较.
还有其他原因需要这种改变吗?
我有一个事件驱动的应用程序.我想保持事件处理程序(EventHandler
具有许多/所有事件的类)是一个常见的实现 - 同时允许EventSource
可更改(特别是在编译时).
为连接EventHandler
用EventSource
,我将不得不存储的实例句柄内EventSource
.我试图存储 各种形式的处理程序:
EventHandler
(即在具体的定义公共处理方法EventHandler
的std::function
- 这提供了最大的灵活性但是,在这两种情况下,调用目标方法/ lambda的延迟都非常高(在我的测试设置上大约250ns) - 更糟糕的是,它是不一致的.可能是由于虚拟表和/或堆分配和/或类型擦除???
为了减少这种延迟,我想使用模板.
我能想到的最好的是:
template <typename EventHandler>
class EventSource1
{
EventHandler* mHandler;
public:
typedef EventHandler EventHandlerType;
void AssignHandler (EventHandler* handler)
{
this->mHandler = handler;
}
void EventuallyDoCallback (int arbArg)
{
this->mHandler->CallbackFunction (arbArg);
}
};
template <EventSourceType>
class EventSourceTraits
{
typedef EventSourceType::EventHandlerType EventHandlerType;
static void AssignHandler (EventSourceType& source, EventHandlerType* handler)
{ …
Run Code Online (Sandbox Code Playgroud) 我与一个比我更聪明的人进行了一次有趣的讨论,我仍然提出了一个关于对齐存储和简单可复制/可破坏类型的公开问题.
请考虑以下示例:
#include <type_traits>
#include <vector>
#include <cassert>
struct type {
using storage_type = std::aligned_storage_t<sizeof(void *), alignof(void *)>;
using fn_type = int(storage_type &);
template<typename T>
static int proto(storage_type &storage) {
static_assert(std::is_trivially_copyable_v<T>);
static_assert(std::is_trivially_destructible_v<T>);
return *reinterpret_cast<T *>(&storage);
}
std::aligned_storage_t<sizeof(void *), alignof(void *)> storage;
fn_type *fn;
bool weak;
};
int main() {
static_assert(std::is_trivially_copyable_v<type>);
static_assert(std::is_trivially_destructible_v<type>);
std::vector<type> vec;
type t1;
new (&t1.storage) char{'c'};
t1.fn = &type::proto<char>;
t1.weak = true;
vec.push_back(t1);
type t2;
new (&t2.storage) int{42};
t2.fn = &type::proto<int>;
t2.weak = false;
vec.push_back(t2);
vec.erase(std::remove_if(vec.begin(), vec.end(), [](const …
Run Code Online (Sandbox Code Playgroud) c++ ×8
c++11 ×3
c++17 ×2
2d ×1
arrays ×1
c++14 ×1
callback ×1
clang ×1
constructor ×1
gcc ×1
javascript ×1
listener ×1
node.js ×1
noexcept ×1
oauth-2.0 ×1
overriding ×1
std-function ×1
vector ×1