当我注意到我找不到 的写出单词时,我正在enum class为 中列出的不同文件标志编写一个保险箱。open(3)O_EXCL
enum class Flags {
readOnly, // O_RDONLY
truncate, // O_TRUNC
? // O_EXCL
};
Run Code Online (Sandbox Code Playgroud)
我想到了两种可能的含义:
OPEN_EXCLUSIVEOPEN_EXISTS_CLOSE但我找不到任何有关其预期含义的资源。
我有一个菜单,所有项目都showAsAction设置为ifRoom。现在我想获得这些菜单项的中心的确切位置 (x,y)。
我首先想到从底层视图中获取这些信息,但我不知道如何可靠地访问特定菜单项的视图。
在onCreateOptionsMenu:
MenuItem someItem = menu.findItem(R.id.some_action);
// someItem.getX();
// someItem.getY();
Run Code Online (Sandbox Code Playgroud)
我希望这些坐标相对于应用程序的Toolbar. 在这种情况下,我还需要访问来someItem.get{Width,Height}()计算中心坐标。
菜单示例:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:title="@string/action_search"
android:icon="@drawable/ic_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
<item
android:id="@+id/action_example"
android:title="@string/action_example"
android:icon="@drawable/ic_example"
app:showAsAction="ifRoom" />
</menu>
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用来自c ++ 17的新推导指南来进行高级类模板参数推导.不幸的是,看起来你只能在之后使用简单的模板声明->,但我需要一个辅助结构来确定结果类型.
我的用例是这样的:我有一个可变参数模板类,它采用任意数量的不同类型.对于一个构造函数,我想指定每一个,对于另一个ctor,我想只指定一个类型并复制N次.
要N在演绎指南中访问它,我引入了一种新类型:
template<size_t N>
struct Replicate { };
Run Code Online (Sandbox Code Playgroud)
我所拥有的课程与此类似:
template<typename... Foos>
struct Foo {
// [ ... ] member std::tuple<Foos...>
// ctor 1: give values for all types (easy to deduce)
Foo(Foos&&... args /* rvalue ref, NOT forwarding */) { };
// ctor 2: give one value and N, result is N copies of this value.
// takes Replicate<N> as parameter to aid the deduction.
template<typename T, size_t N>
Foo(const T& t, Replicate<N>) …Run Code Online (Sandbox Code Playgroud) 在我的源文件中,我有几个代码块,它们有很长的重复出现的部分.以此块为例:
void MemberFunctionBar([..], SomeClass param, [..]) {
/* long common part */
if constexpr (SafeCopy) {
T copyOfParam(param);
/* many lines of code */
Foo(std::move(copyOfParam));
} else {
/* many lines of code (exactly the same as above) */
Foo(param);
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想将这些中间行只写一次,以获得更好的代码可维护性.此外,由于限制,不能进行进一步的功能调用.如果可能,代码应扩展为原始格式.
到目前为止,我正在考虑像这个宏(显然不起作用,因为宏中的预处理程序指令不被解释):
#define IF_SAFE_COPY_COPY_PARAM(block) \
if constexpr (SafeCopy) { \
T paramCopy(param); \
#define __the_param__ std::move(paramCopy) \
block \
#undef __the_param__ \
} else { \
#define __the_param__ param \
block \
#undef __the_param__ \
}
void MemberFunctionBar([..], …Run Code Online (Sandbox Code Playgroud) 我想的参数包折叠N不同类型成std::tuple的N-1 std::pairs具有相应类型.
所以例如表达式
ResolveToTupleOfPairs<void, int, long>::Type tuple;
Run Code Online (Sandbox Code Playgroud)
应该评估
std::tuple<std::pair<void, int>, std::pair<int, long>> tuple;
Run Code Online (Sandbox Code Playgroud)
所以我正在寻找ResolveToTupleOfPairs折叠参数包的类型的实现,如解释的那样.我当前的实现如下,但显然它导致类型是一对对的元组,每个对包含两次相同的类型而不是<T0, T1>, <T1, T2>, ....
template<typename... T>
struct ResolveToTupleOfPairs {
static_assert(sizeof...(Args) > 1, "need at least two arguments");
using Type = std::tuple<std::pair<T, T>...>;
};
Run Code Online (Sandbox Code Playgroud)
我很满意c++17解决方案.
我正在尝试创建一个Invoker对象,该对象为该仿函数存储仿函数和一组参数 - 全部按值(用于线程).
Invoker::operator()()将使用复制的参数调用存储的仿函数.
到目前为止,一切都工作正常,直到尝试通过auto&使用传递参数std::ref(variable).具体来说,此代码应该可以工作,但它不会使用给定的错误消息进行编译:
int var = 0;
Invoker{
[](auto& r) {
printf("%d\n", r);
}, std::ref(var)
}();
Run Code Online (Sandbox Code Playgroud)
我希望它std::thread与这个例子的工作方式类似.错误消息是:
Run Code Online (Sandbox Code Playgroud)test.cpp:65:14: error: no matching function for call to ‘invoke(std::__tuple_element_t<0, std::tuple<main()::<lambda(auto:1&)>, std::reference_wrapper<int> > >, std::__tuple_element_t<1, std::tuple<main()::<lambda(auto:1&)>, std::reference_wrapper<int> > >)’ std::invoke(std::get<Indicies>(std::move(args))...); ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我目前的Invoker课程:
template<typename... Args>
struct Invoker {
std::tuple<std::decay_t<Args>...> args;
explicit Invoker(Args... args)
: args(std::forward<Args>(args)...)
{ }
template<size_t... Indices>
void _Invoke(std::index_sequence<Indices...>) {
std::invoke(std::get<Indices>(std::move(args))...);
}
void operator()() {
_Invoke(std::make_index_sequence<std::tuple_size_v<decltype(args)>>{});
}
};
/* Invoker …Run Code Online (Sandbox Code Playgroud)