小编Cur*_*ous的帖子

如何展开模板专业化

我试图使用模板元编程在参数包中的指定索引处获取类型.我有下面的代码,但由于某种原因它总是返回一个int,有人可以告诉我我做错了什么?

#include <string>
#include <iostream>
using std::cout;
using std::endl;
using std::string;

template <int current_index, typename... Vs>
struct TypeForIndex {};
template <int current_index, typename Head, typename... Tail>
struct TypeForIndex<current_index, Head, Tail...> : private TypeForIndex<current_index + 1> {
    using type = Head;
};
template <int current_index, typename Tail>
struct TypeForIndex<current_index, Tail> {
    using type = Tail;
};

int main() {

    TypeForIndex <2, int, double, string>::type a {"hello"};
    cout << a << endl;

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

上面的代码应该string作为类型返回,a但不知何故它总是一个 …

c++ templates types variadic-templates c++11

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

为什么存在shared_ptr的原子重载

为什么有用于原子重载shared_ptr所描述这里,而不是那里是一个专门用于std::atomic与交易shared_ptr秒.似乎与C++标准库的其余部分采用的面向对象模式不一致.

并且只是为了确保我做对了,当使用shared_ptrs来实现读取副本更新时,我们需要通过这些函数对共享指针进行所有访问(读取和写入)吗?

c++ smart-pointers shared-ptr c++11

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

具有std ::对象数组的类,没有默认构造函数

所以我们假设我有以下课程

class NoDefaultConstructor {
    NoDefaultConstructor() = delete;
    ...
};
Run Code Online (Sandbox Code Playgroud)

我有另一个类,有一个类型NoDefaultConstructor和其他成员的数组

class Wrapper {
    std::array<NoDefaultConstructor, 2> arr;
    ... 
};
Run Code Online (Sandbox Code Playgroud)

如何在构造函数中初始化数组Wrapper(可能在初始化列表中使用std::intializer_list)?

更具体地说,是我可以将参数传递给初始化列表中的数组构造函数Wrapper以获得类似于以下的构造的唯一方法吗?我正在考虑这样做,因为数组的大小将来可能会改变.

template <typename... Values>
Wrapper(Values&&... values) : arr{std::forward<Values>(values)...} {}
Run Code Online (Sandbox Code Playgroud)

c++ arrays uniform-initialization c++11

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

Pythonic方法检查整数是否适合64位

什么是Pythonic方式(不使用任何外部库)来检查整数是否足够小以适合64位有符号数量?

对不起,如果以前问过这个问题!

python 64-bit integer

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

使用Travis服务器上的Buck构建

我正在尝试使用Travis CI服务器上的C++目标(通过GitHub)与Buck构建测试buck test,但有两件事我无法弄清楚.

  1. 我必须在Travis服务器上安装Buck.我如何使用OS X服务器而不是Ubuntu服务器?我在Travis文档中读到它也提供了OS X服务器.这样我就可以继续做brew tap facebook/fb && brew install buck,我会完成.
  2. Buck要求我指定要.buckconfig在存储库中的文件中使用的编译器.但是,这只接受编译器使用的绝对路径.Travis CI配置似乎只提供包含编译器路径的环境变量.但环境变量不能用于.buckconfig.有没有办法可以安装一个编译器(一个完全支持C++ 14的编译器)并获得它在Travis服务器上的安装路径?

我强调的问题,以便它是没有与所有我提到的(巴克和Travis CI)仍回答这个问题的事情体验谁的人更容易.

c++ github travis-ci c++14 buck

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

如果传入任何类型的列表,尝试禁用函数

我试图禁用一个函数,如果任何类型的列表类传递给该函数与以下enable_if

template <typename ContainerType, typename KeyType,
          typename = std::enable_if_t<!std::is_same<
            std::decay_t<ContainerType>,
            std::list<typename ContainerType::value_type>>::value>>
void func(ContainerType&& container, KeyType&& key)
Run Code Online (Sandbox Code Playgroud)

但是当我调用func时, vector<int>我得到了错误

candidate template ignored: substitution failure [with ContainerType = std::__1::vector<int, std::__1::allocator<int> > &, KeyType = int]: type 'std::__1::vector<int, std::__1::allocator<int> > &' cannot be used prior to '::' because it has no
  members
Run Code Online (Sandbox Code Playgroud)

向量确实有成员typedef value_type来获取存储在其中的东西的值.

知道如何解决这个问题吗?

c++ sfinae template-meta-programming

4
推荐指数
2
解决办法
79
查看次数

std :: set迭代器的冲突返回类型

这是今天的第二个编译错误让我感到困惑.不知何故,对于下面的代码,gcc抱怨代码具有返回迭代器return_iter返回冲突类型的函数std::_Rb_tree_iterator<const int*>然后std::_Rb_tree_const_iterator<const int*>,但是它们都不应该是const迭代器,因为set不是const.任何人都可以解释为什么在std::end()非const lvalue上调用该方法会返回一个const_iterator

完整的代码粘贴在下面.

注意我只在编译时遇到此错误gcc.当我用clang编译它时,这个错误没有出现(Apple LLVM version 8.0.0 (clang-800.0.38).我正在使用的gcc版本是g++ (GCC) 5.1.0

一个相关的问题.这是正确使用前进吗?std::forward每当您想使用转发参考时,是否可以打电话?我在下面调用它的原因是为了防止类型在对象是rvalue时重载某些方法.


#include <vector>
#include <string>
#include <set>
#include <iostream>
using namespace std;

int global_value = 1;

class LessPtr {
public:

    template <typename PointerComparableOne, typename PointerComparableTwo>
    constexpr auto operator()(PointerComparableOne&& lhs,
                              PointerComparableTwo&& rhs) const {
        return *std::forward<PointerComparableOne>(lhs) <
            *std::forward<PointerComparableTwo>(rhs);
    }

    using is_transparent = std::less<void>::is_transparent;
};

template <typename Container, typename Key>
auto return_iter(Container&& …
Run Code Online (Sandbox Code Playgroud)

c++ gcc const-iterator auto c++11

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

用户定义的文字定义

我正在查看用户定义的文字的cppreference页面,我想除了几个例子我理解了所有内容

template <char...> double operator "" _?(); // OK
Run Code Online (Sandbox Code Playgroud)

这个操作符如何工作?你怎么称呼它?

double operator"" _Z(long double); // error: all names that begin with underscore
                                   // followed by uppercase letter are reserved
double operator""_Z(long double); // OK: even though _Z is reserved ""_Z is allowed
Run Code Online (Sandbox Code Playgroud)

上述两个功能有什么区别?如果第一个函数不是错误,那么调用第一个函数而不是第二个函数会有什么不同?

谢谢!

c++ user-defined-literals c++11

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

如何检查模板模板类的接口

我试图根据模板模板参数是否具有在其中type定义的类型(例如,std::remove_reference具有type成员类型别名)来使用SFINAE重载模板类,但我无法找到一个好的方法来执行此操作.

例如,我想做

template <template <typename...> class Trait>
using EnableIfHasTypeMember = std::void_t<Trait::type>;

template <template <typename...> class Trait, typename OtherStuff,
          EnableIfHasTypeMember<Trait>* = nullptr>
class Something { ... }
Run Code Online (Sandbox Code Playgroud)

但这给了我一个编译器错误.有什么办法可以检查模板模板参数的界面吗?

c++ templates template-templates c++11 c++14

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

为什么调用转发引用构造函数而不是复制构造函数?

鉴于以下代码

#include <iostream>

using namespace std;

template <typename Type>
struct Something {
    Something() {
        cout << "Something()" << endl;
    }

    template <typename SomethingType>
    Something(SomethingType&&) {
        cout << "Something(SomethingType&&)" << endl;
    }
};

int main() {
    Something<int> something_else{Something<int>{}};
    auto something = Something<int>{};
    Something<int>{something};
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我得到以下输出

Something()
Something()
Something(SomethingType&&)
Run Code Online (Sandbox Code Playgroud)

为什么复制构造函数被解析为模板化转发引用构造函数而不是移动构造函数?我猜这是因为移动构造函数是隐式定义的,而不是复制构造函数。但是在阅读了堆栈溢出中未隐式定义复制构造函数的情况后,我仍然感到困惑。

c++ constructor c++11 c++14 implicit-constructor

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