小编Afs*_*hin的帖子

我需要编写第二个 SFINAE 构造函数吗?

当参数包中的所有类型都不同时,我想激活带有参数包的类。我编写了一个像这样的小辅助函数,它可以正常工作:

template<typename T, typename... Ts>
constexpr bool has_duplicate_types() {
    if constexpr (sizeof...(Ts) == 0)
        return false;
    else
        return ((std::is_same_v<std::decay_t<T>, std::decay_t<Ts>> || ...) || has_duplicate_types<Ts...>());
}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用 SFINAE 激活我的课程时,我尝试了几种方法,有效的方法是:

template<typename T, typename dummy = void, typename... Ts>
struct XYZ_imp {
    XYZ_imp() {
        std::cout << "ok\n";
    }
};

template<typename T, typename... Ts>
struct XYZ_imp<T, std::enable_if_t<has_duplicate_types<T, Ts...>()>, Ts...> {
    XYZ_imp() = delete;
};

template<typename T, typename... Ts>
using XYZ = XYZ_imp<T, void, Ts...>;
Run Code Online (Sandbox Code Playgroud)

但正如您所看到的,代码太多了。但是当我尝试这样写时:

template<typename T, typename... Ts>
struct XYZ {
    template<typename U …
Run Code Online (Sandbox Code Playgroud)

c++ sfinae

5
推荐指数
1
解决办法
85
查看次数

ConstraintLayout行为从1.1.2到1.1.3的奇怪变化

我注意到ConstraintLayout行为从1.1.2到1.1.3发生了奇怪的变化,这可能会导致布局出现很多问题。另外,我个人认为这是一个错误,因为这种行为应该是错误的。

检查以下布局:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">    
<Button
    android:id="@+id/test1_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="test1"
    app:layout_constraintTop_toTopOf="@id/test2_btn"
    app:layout_constraintBottom_toBottomOf="@id/test2_btn"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toStartOf="@id/test2_btn"
    app:layout_constraintHorizontal_chainStyle="spread_inside"
    app:layout_constraintHorizontal_bias="1"/>

<Button
    android:id="@+id/test2_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="test2"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@id/test1_btn"/>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

这是在版本1.1.2和版本中呈现此布局1.1.3的方式ConstraintLayout

在此处输入图片说明

现在我们添加android:visibility="gone"test1_btn。在ConstraintLayoutversion中1.1.2,布局呈现如下:

在此处输入图片说明

这是完全合乎逻辑的,因为我们已经app:layout_constraintHorizontal_bias="1"设置好了,所以test2_btn应该保持在链的最右边。现在,如果我们使用ConstraintLayoutversion 1.1.3,则布局将如下所示:

在此处输入图片说明

到底发生了什么?为什么我失去了连锁偏见?

android android-constraintlayout

4
推荐指数
1
解决办法
515
查看次数

为什么指向函数的指针等于1?

检查以下代码:

#include <iostream>
using namespace std;

int& foo() {
    static int i = 0;
    return i;
}

int main() {
    cout << &foo() << endl;
    cout << &foo << endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

正如你看到的,第一个cout返回值的打印地址foo(),这将是静态变量i里面foo().对于第二个cout我期待那&foo的回报地址foo()功能,如规定在这里:

2)如果操作数是非静态成员的限定名,例如&C :: member,则结果是指向成员函数的prvalue指针或指向类C中类型T的数据成员的指针.注意,&member和C都不是: :member甚至&(C :: member)可用于初始化指向成员的指针.

但令我惊讶的是,这是我的输出:

0x5650dc8dc174
1
Run Code Online (Sandbox Code Playgroud)

第一个是好的,但第二个是1?怎么回事?为了确保我没有弄乱任何东西,我在下面编写了这段代码C:

#include <stdio.h>

int foo() {
}

int main(void) {
    printf("%p", &foo);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

以下输出:

0x55732bd426f0 …
Run Code Online (Sandbox Code Playgroud)

c++ function-pointers implicit-conversion

3
推荐指数
1
解决办法
153
查看次数

GCC/Clang 中这个奇怪的表达是什么?

我最近注意到GCC/Clang中有一个奇怪的有效C/C++表达式,这是我以前从未见过的。这是C++中的示例,但类似的表达式也适用于C

int main(){
    int z = 5;
    auto x = ({z > 3 ? 3 : 2;}); // <-- expression
    std::cout << x;
}
Run Code Online (Sandbox Code Playgroud)

它的作用在某种程度上是显而易见的,但我想知道它叫什么。由于它在MSVC中不值得,我猜它是一个非标准扩展。但是有什么东西也适用于MSVC吗?特别是在C

c c++ gcc-statement-expression

3
推荐指数
1
解决办法
513
查看次数

使用lower_bound/upper_bound有2种不同的类型

我有一个小的工作代码,用于查找使用特殊比较方法的一系列项目.但是,当我尝试重写它lower_bound()upper_bound()功能,我得到一个奇怪的错误.我写了一个小代码来显示我的问题.这是代码:

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>

using namespace std;

int main() {
    string str = "banana";
    string keyword = "ana";
    int sa_idx[] = {5, 3, 1, 0, 4, 2};
    vector<int> sa(sa_idx, sa_idx + sizeof(sa_idx) / sizeof(int) );

    auto cmp = [&str] (const int &a, const string &keyword) -> bool
    {
        return strncmp(str.c_str() + a,  keyword.c_str(), keyword.length()) < 0;
    };

    cout << (upper_bound(sa.begin(), sa.end(), keyword, cmp) - 
        lower_bound(sa.begin(), sa.end(), keyword, cmp)) << endl;

    return …
Run Code Online (Sandbox Code Playgroud)

c++

2
推荐指数
1
解决办法
142
查看次数

这个 C++ ref 示例有问题吗?

我在检查aligned_storagecppref,但我认为它的例子是越野车。这是代码:

#include <iostream>
#include <type_traits>
#include <string>
 
template<class T, std::size_t N>
class static_vector
{
    // properly aligned uninitialized storage for N T's
    typename std::aligned_storage<sizeof(T), alignof(T)>::type data[N];
    std::size_t m_size = 0;
 
public:
    // Create an object in aligned storage
    template<typename ...Args> void emplace_back(Args&&... args) 
    {
        if( m_size >= N ) // possible error handling
            throw std::bad_alloc{};
 
        // construct value in memory of aligned storage
        // using inplace operator new
        new(&data[m_size]) T(std::forward<Args>(args)...);
        ++m_size;
    }
 
    // Access an object …
Run Code Online (Sandbox Code Playgroud)

c++

2
推荐指数
1
解决办法
99
查看次数

为什么类成员的类型推导失败?

假设我们有这样的小代码:

template<typename T>
struct Test {
    Test(T t) : m_t(t) {}
    T m_t;
};

int main() {
    Test t = 1;
}
Run Code Online (Sandbox Code Playgroud)

这段代码很容易用[T=int]for Testclass 进行编译。现在如果我写这样的代码:

template<typename T>
struct Test {
    Test(T t) : m_t(t) {}
    T m_t;
};

struct S {
    Test t = 1;
};

int main() {
    S s;
}
Run Code Online (Sandbox Code Playgroud)

此代码无法编译并出现以下错误:

invalid use of template-name 'Test' without an argument list
Run Code Online (Sandbox Code Playgroud)

我需要像Test<int> t = 1;班员一样写它才能工作。知道为什么会发生这种情况吗?

c++ c++17

2
推荐指数
1
解决办法
51
查看次数

c ++ auto variable(map)被分配垃圾值

我尝试分配 'a' 变量如下:

for (auto& a : getMap()[1])
Run Code Online (Sandbox Code Playgroud)

将垃圾值分配给a. 但是如果我在如下所示首先声明变量后使用它,它可以正常工作。

auto vv = getMap()[1];
    for (auto& a : vv)
Run Code Online (Sandbox Code Playgroud)

如果我在没有声明变量的情况下立即使用它,为什么会出现问题?

#include <string>
#include <map>
#include <memory>
#include <vector>
using namespace std;

typedef struct _mystruct {

} mystruct;

map<int, vector<shared_ptr<mystruct>>> mymap;

void init() {
    vector<shared_ptr<mystruct>> v;
    v.push_back(make_shared<mystruct>(mystruct()));
    mymap[1] = v;
}

map<int, vector<shared_ptr<mystruct>>> getMap() {
    return mymap;
}

int main()
{
    init();

    vector<shared_ptr<mystruct>> v2;
    for (auto& a : getMap()[1]) {
        v2.push_back(a);
    }

    auto vv = getMap()[1];
    for (auto& a …
Run Code Online (Sandbox Code Playgroud)

c++ auto

0
推荐指数
1
解决办法
78
查看次数