小编T.C*_*.C.的帖子

将秒转换为小时,分钟和秒

#include <cstdio>
#include <iostream>
using namespace std;
int main ()
{
    int seconds, hours, minutes;
    cin >> seconds;
    hours = seconds/3600;
    cout << seconds << " seconds is equivalent to " << int(hours) << " hours " << seconds%(hours*60) 
         << " minutes " << (seconds%(hours*3600))-((seconds%(hours*60))*60) << " seconds.";
}
Run Code Online (Sandbox Code Playgroud)

出于某种原因,此程序仅适用于3600以上的数字.有谁知道如何解决这个问题?每当我执行低于3600的数字时,屏幕会显示一条来自Windows的消息,说该程序已停止工作.

c++

12
推荐指数
2
解决办法
8万
查看次数

pack(类型擦除)随机数生成器

C++ 11 std库有几个随机数生成器(RNG),每个生成器实现UniformRandomNumberGenerator概念.然后可以将它们用作随机分布的参数,另请参阅此文档以获取概述.

这种设计的优点是底层RNG引擎的选择与其应用分离.但是,该设计还要求对RNG的所有调用的定义(不仅仅是声明)是可用的(如果RNG类型仍未指定为模板参数).因此,在

struct complicated_random_distribution
{
  /*
     some data and auxiliary methods here
  */
  // complicated; may call RNG::operator() many times
  template<typename RNG>
  some_type operator()(RNG&gen) const;
};
Run Code Online (Sandbox Code Playgroud)

该成员operator()不能直接在单独的编译单元(CU)中实现,但必须在相同的头文件中(或#include从其中一个d)中可用.

对于单独的实现,理想情况下,想要某种方式来打包RNG的方式与std::function<>打包任何可调用对象的方式相同.(简单地使用std::function和提供单独CU中定义的函数的值RNG::min()RNG::max()作为参数是限制性的,并且不允许使用,例如,std::uniform_real_distribution<>在内部).

如何才能做到这一点?这个实现是否可用?std库将来会提供这个吗?还是我在吃了一只红鲱鱼?


编辑随机数生成器需要有static成员,min()并且max()使类型擦除变得困难或不可能(GNU的libstdc ++没有做出这种假设,并且使用非静态成员进行类型擦除min()max()工作,但不能使用LLVM的libc ++,它使用所需的标准static成员).还有办法还能解决这个问题吗?如果没有,这是否意味着C++标准对随机数生成器有一个拙劣的界面?

c++ random c++11

12
推荐指数
1
解决办法
387
查看次数

C++中的可扩展类型特征

我想编写一个通用的序列化库,它提供了一个通用的save函数.该库包含自定义类型特征,例如some_condition:

template <typename T>
struct some_condition
{
    constexpr static bool value = std::is_same<std::string, T>::value ||std::is_arithmetic<T>::value ;
};
Run Code Online (Sandbox Code Playgroud)

save的行为选择基于some_condition:

template <typename T>
std::enable_if_t<some_condition<T>::value> save(const T& value)
{
    std::cout << "these types will be handled in a specific way: " << value << std::endl;
}

template <typename T>
std::enable_if_t<!some_condition<T>::value> save(const T& value)
{
    std::cout << "these types will be handled in another way: " << value << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

save应该可以为用户数据类型进行自定义,不仅可以通过重载,还可以通过特征进行自定义.因此我创建了trait_extension哪些可以专门用于特征模板:

template <template<typename> …
Run Code Online (Sandbox Code Playgroud)

c++ templates type-traits c++14

12
推荐指数
1
解决办法
1119
查看次数

max_digits10的目的是什么?它与digits10的不同之处是什么?

我对什么max_digits10代表感到困惑.根据其文档,对于所有整数类型,它为0.浮点类型的公式max_digits10看起来类似于int's digits10'.

c++ precision numeric-limits floating-point-precision c++11

11
推荐指数
2
解决办法
2404
查看次数

包含与非平凡成员的联合的类的构造函数和复制构造函数

我正在尝试实现一个自定义变体类型,它使用union来存储各种不同类型的数据.在该领域,type_id我计划存储联合中存储的数据的类型.工会包含非平凡的成员.这是我目前的实施:

struct MyVariant {
  enum { t_invalid, t_string, t_int, t_double, t_ptr, t_dictionary } type_id;
  union {
    int                             as_int;
    double                          as_double;
    std::string                     as_string;
    std::unique_ptr<int>            as_ptr;
    std::map<int, double>           as_dictionary;
  };
};
Run Code Online (Sandbox Code Playgroud)

我尝试创建一个MyVariant类似如下的实例:

MyVariant v;
Run Code Online (Sandbox Code Playgroud)

我收到错误消息:调用隐式删除的MyVariant默认构造函数.所以,我尝试手动实现构造函数,如下所示:

MyVariant() : type_id{t_int}, as_int{0} {}
Run Code Online (Sandbox Code Playgroud)

这给了我一个类似的错误消息:尝试使用已删除的功能.接下来,我尝试实现以下构造函数:

MyVariant(int value) : type_id{t_int}, as_int{value} {}
Run Code Online (Sandbox Code Playgroud)

并构造我的实例如下:

MyVariant v{123};
Run Code Online (Sandbox Code Playgroud)

=>相同的错误消息:尝试使用已删除的功能.

我也开始实现一个拷贝构造函数,它看起来如下.但是,当然这对编译器错误没有帮助.

MyVariant::MyVariant(const MyVariant& other)
{
    type_id = other.type_id;
    switch (type_id) {
        case t_invalid:
            break;
        case t_string:
            new (&as_string) std::string();
            as_string = …
Run Code Online (Sandbox Code Playgroud)

c++ constructor unions c++11 c++14

11
推荐指数
1
解决办法
2953
查看次数

如何在C++ 11中访问可能不存在的类型别名?

我想写这样的东西:

template <class T>
using MyTypeOrTupple = typename std::conditional<has_member_type_MyType<T>::value,
                                                 typename T::MyType,
                                                 std::tuple<> >::type;
Run Code Online (Sandbox Code Playgroud)

has_member_type_MyType使用https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Member_Detector实现

然而,海湾合作委员会(4.8.4)仍然抱怨使用T::MyType何时MyType未定义T.有办法解决这个问题吗?

c++ templates c++11

11
推荐指数
2
解决办法
792
查看次数

这是std :: get_time中的错误吗?

我试图解析日期时间字符串并将结果放入std :: tm结构.下面是代码,

#include <iomanip>
#include <ctime>
#include <sstream>
#include <string>

std::stringstream ss;
struct std::tm when;

ss.str("8/14/2015 3:04:23 PM");
ss >> std::get_time(&when, "%m/%d/%Y %r");
Run Code Online (Sandbox Code Playgroud)

运行代码后,when.tm_hour是27.这是一个错误,还是我做错了什么?

我在Windows 7上使用Visual Studio 2013.

谢谢.

c++ c++11

11
推荐指数
1
解决办法
1345
查看次数

std :: vector在前向声明的类型上

以下代码似乎在Clang ++和GCC上正常工作:

#include <vector>

class A {
private:
    int i;
    std::vector<A> children;
public:
    A& add();
};

A& A::add() { children.emplace_back(); return children.back(); }

int main() {
    A a;
    A& a2 = a.add();
}
Run Code Online (Sandbox Code Playgroud)

std::vector<A>声明数据成员时,A仍然是不完整的类型.同样在使用std::vector<B>B只向前声明class B;.它应该工作,std::vector因为它只包含一个指针A.

这是保证工作还是未定义的行为?

c++ stl forward-declaration c++11 c++17

10
推荐指数
1
解决办法
1051
查看次数

默认移动构造函数和引用成员

从N3337的[12.8] [11]:

非联合类X的隐式定义的复制/移动构造函数执行其基础和成员的成员复制/移动.[注意:忽略非静态数据成员的大括号或大小写.另请参见12.6.2中的示例.-end note]初始化顺序与用户定义构造函数中基数和成员的初始化顺序相同(见12.6.2).让我们x为构造函数的任何参数,或者对于移动构造函数,x值指的是参数.以适合其类型的方式复制/移动每个基本或非静态数据成员:

- 如果成员是一个数组,则使用x的相应子对象直接初始化每个元素;

- 如果成员m具有右值引用类型T &&,则直接初始化为static_cast<T&&>(x.m);

- 否则,用x的相应基数或成员直接初始化基数或成员.

这实际上是一个澄清,但我看不到在该子句中提到左值引用成员.由于它没有提到它们,默认情况下似乎说它们是隐式成员移动的一部分,但是以下方法不起作用;

int x = 5;
int& y = x;
int& z(std::move(y)); //error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'std::remove_reference<int&>::type {aka int}'
Run Code Online (Sandbox Code Playgroud)

因此,可以安全地假设默认的移动构造函数区分成员是一个引用而且只是这样做

int& z = y;
Run Code Online (Sandbox Code Playgroud)

没有电话std::move

c++ constructor reference move c++11

10
推荐指数
1
解决办法
2222
查看次数

是否有__attribute __((packed))的C++ 11/14替代方案

C++ 11引入了标准化属性语法的概念.我找不到所有支持的属性的列表.是否有C++ 11属性替代__attribute__((packed))

例如

struct __attribute__((packed)) Pack {
   uint8_t field1;
   uint8_t field2;
};
Run Code Online (Sandbox Code Playgroud)

c++ gcc clang c++11 c++14

10
推荐指数
1
解决办法
2342
查看次数