在我的第一个例子中,我使用了两个位域int64_t.当我编译并获得类的大小时,我得到8.
class Test
{
int64_t first : 40;
int64_t second : 24;
};
int main()
{
std::cout << sizeof(Test); // 8
}
Run Code Online (Sandbox Code Playgroud)
但是,当我将第二个bitfeild更改int32_t为类的大小时,会增加到16:
class Test
{
int64_t first : 40;
int32_t second : 24;
};
int main()
{
std::cout << sizeof(Test); // 16
}
Run Code Online (Sandbox Code Playgroud)
这种情况发生在GCC 5.3.0和MSVC 2015上.但为什么呢?
在导航图中定义的示例导航操作中:
<action
android:id="@+id/action_fragment1_to_fragment2"
app:destination="@id/fragment2"
app:enterAnim="@anim/right_slide_in"
app:popExitAnim="@anim/left_slide_out"/>
Run Code Online (Sandbox Code Playgroud)
当Fragment2打开并开始从右侧滑入视图时,其Fragment1立即消失(可悲)。当Fragment2close关闭并开始向右滑动时,Fragment1在它下面可以很好地看到,从而提供很好的堆栈弹出效果(与iOS相比)。
滑入视图Fragment1时如何保持可见Fragment2?
java animation android android-fragments android-architecture-navigation
这是最小的例子:
class A
{
A* const& this_ref;
public:
A() : this_ref(this) {}
};
Run Code Online (Sandbox Code Playgroud)
GCC 5.3.0发出警告:
警告:临时绑定到'A :: this_ref'只会持续存在,直到构造函数退出[-Wextra] A():this_ref(this){}
那是this暂时的吗?什么... MSVC 2015对此保持沉默,并且this_ref->member在我的情况下在构造函数之外引用类成员给出了预期的行为(但可能仅仅是UB的情况,不确定).
编辑:
请注意,这个问题扩展了一个链接尽可能重复,因为它不是关于创建此类引用的方法的一般性问题,而是关于警告GCC(以及除MSVC之外的其他可能的编译器)在创建时产生的.
这是完整的例子:
auto callSelf = [](auto& func) {func(func);};
class wrapper : public decltype(callSelf) {
using base = decltype(callSelf);
public:
wrapper() : base(callSelf) {}
template<class T>
void operator()(T& func) {
base::operator()(func);
}
};
int main()
{
//callSelf(callSelf); // Error
wrapper w;
w(w); // OK, nice endless recursion
}
Run Code Online (Sandbox Code Playgroud)
为什么可以使用包装器,而直接执行它会导致以下错误?
main.cpp:30: error: use of '<lambda(auto:1&)> [with auto:1 = <lambda(auto:1&)>]' before deduction of 'auto'
auto callSelf = [&](auto& func) {func(func);};
~~~~^~~~~~
Run Code Online (Sandbox Code Playgroud) 我尝试实现一种功能,允许用户选择默认的Android默认启动器应用程序.此外,我需要接收选择了哪个应用程序的信息.但这种方法存在问题.
要让用户选择Launcher Application,我们可以简单地启动给定的intent:
val selector = Intent(Intent.ACTION_MAIN)
selector.addCategory(Intent.CATEGORY_HOME)
selector.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(selector)
Run Code Online (Sandbox Code Playgroud)
它导致这样的对话:
我观察到的,如果我使用startActivity,Launcher应用程序设置很好并按预期工作,但如果我使用startActivityForResult,那么我将得到一些回调,但Launcher应用程序根本不会设置.此外,收到的意图并没有什么有趣的onActivityResult.
然后,我尝试使用IntentSender.
代码如下:
val selector = Intent(Intent.ACTION_MAIN)
selector.addCategory(Intent.CATEGORY_HOME)
selector.flags = Intent.FLAG_ACTIVITY_NEW_TASK
val receiver = Intent(this, MyBroadcastReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(this, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT)
val chooser = Intent.createChooser(selector, "Select a Home app", pendingIntent.intentSender);
startActivity(chooser)
Run Code Online (Sandbox Code Playgroud)
接收器如下:
class MyBroadcastReceiver: BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val componentName = intent.extras.getParcelable<ComponentName>(Intent.EXTRA_CHOSEN_COMPONENT)
//com.example.myapp if my app was choosen
val pkg = componentName.packageName …Run Code Online (Sandbox Code Playgroud) 我想在编译时检查是否_name为类型Ret和参数定义了用户文字Arg.虽然我有半解决方案,但它需要operator至少定义一次文字:
#include <iostream>
#include <type_traits>
struct one { };
struct two { };
// we need at least one of these definitions for template below to compile
one operator"" _x(char const*) {return {};}
two operator"" _x(unsigned long long int) {return {};}
template<class T, class S, class = void>
struct has_literal_x : std::false_type
{ };
template<class T, class S>
struct has_literal_x <T, S,
std::void_t<decltype((T(*)(S))(operator"" _x))>
> : std::true_type
{ };
int main()
{ …Run Code Online (Sandbox Code Playgroud) 这个问题的灵感来自于这个答案.我想知道在给定标准中简化它的最佳方法是什么.一个我知道并且个人使用/仍在使用,因为C++ 14是宏REQUIRES(x):
定义:
template<long N>
struct requires_enum
{
enum class type
{
none,
all
};
};
#define REQUIRES(...) requires_enum<__LINE__>::type = \
requires_enum<__LINE__>::type::none, \
bool PrivateBool = true, \
typename std::enable_if<PrivateBool && (__VA_ARGS__), int>::type = 0
Run Code Online (Sandbox Code Playgroud)
即使对于非模板化函数调用也使用:
template<REQUIRES(sizeof(int)==4)>
int fun() {return 0;}
int main()
{
fun(); //only if sizeof(int)==4
}
Run Code Online (Sandbox Code Playgroud)
REQUIRES我使用的原文来自这篇文章.
还有什么好的技巧?
SFINAE的一些例子需要一些或很长时间才能理解读者刚开始使用SFINAE的冒险:
Pre-C++ 11 SFINAE示例(来源):
template <typename T>
struct has_typedef_foobar {
// Types "yes" and "no" are guaranteed to have different …Run Code Online (Sandbox Code Playgroud) 我们可以检测员function template,variable template, class/ struct/ union template或alias template不知道量或性质template/ non-template参数?
当我试着考虑这个时,没有什么真正想到的.但是让我们有成员函数模板的结构:
struct foo
{
// Really random. Let's assume we don't know this declaration, just the name "bar"
template <class T, std::size_t N, class... Args>
void bar(T a, T b, T(&c)[N], Args const& ...);
};
Run Code Online (Sandbox Code Playgroud)
如何检查foo::bar模板是否存在?
基于实例化的类型特征在这里不适用,因为(理论上)我们不知道应该使用哪些参数,按什么顺序以及有多少参数.也许一些神奇的查找方法是合适的?或者也许这是不可能的?
在搜索时,我发现了这个问题,但答案中的解决方案需要有关性质的知识template.
这是我第一次尝试检测失败struct template:
struct foo
{ …Run Code Online (Sandbox Code Playgroud) 如果所有(或某些)参数都为null,kotlin中是否有一种方法可以阻止函数调用?例如具有功能:
fun test(a: Int, b: Int) { /* function body here */ }
Run Code Online (Sandbox Code Playgroud)
我希望在参数出现时防止空检查null.例如,对于参数:
val a: Int? = null
val b: Int? = null
Run Code Online (Sandbox Code Playgroud)
我想替换:
a?.let { b?.let { test(a, b) } }
Run Code Online (Sandbox Code Playgroud)
有:
test(a, b)
Run Code Online (Sandbox Code Playgroud)
我想函数定义语法看起来像这样:
fun test(@PreventNullCall a: Int, @PreventNullCall b: Int)
Run Code Online (Sandbox Code Playgroud)
这相当于:
fun test(a: Int?, b: Int?) {
if(a == null) {
return
}
if(b == null) {
return
}
// function body here
}
Run Code Online (Sandbox Code Playgroud)
类似的东西(或类似的东西)是否可以减少调用者(可能是函数作者)的冗余代码?
新的ViewBinding与带有合成视图绑定的Kotlin Android扩展相比如何?
除了新ViewBindings提供的NullSafety和TypeSafety之外,我们为什么还要考虑放弃在Views上使用合成绑定的Kotlin方法。
由于新的ViewBinding事先生成了Binding类,因此它的性能更高吗?
android kotlin kotlin-android-extensions android-viewbinding