小编Ale*_*k86的帖子

私人模板功能

我有一节课:

class C {
private:
  template<int i>
  void Func();

  // a lot of other functions
};
Run Code Online (Sandbox Code Playgroud)

C.cpp

// a lot of other functions

template<int i>
void C::Func() {
 // the implementation
}

// a lot of other functions
Run Code Online (Sandbox Code Playgroud)

我知道,在cpp文件中移动模板实现并不是最好的主意(因为它不会从其他cpp中看到,它可能包含带有模板声明的标头).

但私人功能怎么样?谁能告诉我是否有在.cpp文件中实现私有模板功能的缺点?

c++ implementation templates private

18
推荐指数
2
解决办法
6272
查看次数

boost:从当前时区获取当前local_date_time

问题是:

  • 我知道如何让当地时间得到提升

代码:

    boost::local_time::local_date_time currentTime(
        boost::posix_time::second_clock::local_time(),
        boost::local_time::time_zone_ptr());
    std::cout << currentTime.local_time() << std::endl;
Run Code Online (Sandbox Code Playgroud)
  • 我知道如何从机器获取当前时区数据(我希望它是正确的方式)

代码:

tzset();
// the var tzname will have time zone names
// the var timezone will have the current offset
// the var daylight should show me if there is daylight "on"
Run Code Online (Sandbox Code Playgroud)

但是我仍然无法使用当前的time_zone获取local_date_time ...有人知道,怎么做?

c++ timezone boost boost-date-time

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

是否有警告(错误),类似于C#C4061 for C#

通常,如果我在C#中使用switch for enums,我必须编写类似的东西:

switch (e)
{
    case E.Value1:
        //...
        break;

    case E.Value2:
        //...
        break;

        //...
    default:
        throw new NotImplementedException("...");
}
Run Code Online (Sandbox Code Playgroud)

在C++(对于VS)中,我可以为此开关启用警告C4061和C4062,使它们出错并进行编译时检查.在C#中,我必须将此检查移至运行时...

有谁知道在C#中如何在编译时检查这个?也许有一个警告,默认情况下禁用,我错过了,或者其他方式?

c# enums switch-statement compile-time

7
推荐指数
1
解决办法
946
查看次数

如何解析带有日期时间+时间偏移的字符串到boost :: posix_time :: ptime?

我有一个字符串"2011-10-20T09:30:10-05:00"

有人知道如何使用boost :: date_time库解析它吗?

c++ boost boost-date-time

6
推荐指数
1
解决办法
2561
查看次数

C++私有嵌套类 - 访问不同的函数

发现这个奇怪的编译行为,检查VS2012,VS2017和https://www.onlinegdb.com/online_c++_compiler)

基本上,对于私有嵌套类,您可以在外部调用公共函数,但不能调用公共构造函数.

3个问题:

  • 编译器让我调用func()的原因是什么?

  • 如果编译器让我调用func(),为什么我不能调用ctor?

  • 如果我不能打电话给ctor,为什么emplace_back能够做到呢?


class Outer {
    struct PrivateInner {
        PrivateInner() {}
        void func() {}
    };
public:
    PrivateInner inner;
    std::vector<PrivateInner> innerVect;
};

void f1()
{
    Outer c;
    c.inner.func(); // COMPILING, but why?
}

void f2()
{
    Outer c;
    c.innerVect.push_back(Outer::PrivateInner()); // NOT COMPILING, why no access to ctor if there is access to func()?
    c.innerVect.emplace_back(); // COMPILING, but why? Doesn't it call Outer::PrivateInner inside?
}
Run Code Online (Sandbox Code Playgroud)

正如我所见,我仍然可以创建一个(静态)函数createObject():

class Outer {
    struct PrivateInner {
        PrivateInner() {}
        static PrivateInner createObject() …
Run Code Online (Sandbox Code Playgroud)

c++ private class

6
推荐指数
1
解决办法
2606
查看次数

为什么即使在非常简单的情况下,挥发性变量也没有优化?

如果我编译代码

int main()
{
    int i;
    i = 1;
    i = 2;
}
Run Code Online (Sandbox Code Playgroud)

在具有发布和优化的VS中,反汇编看起来像:

int main()
{
    int i;
    i = 1;
    i = 2;
}
010D1000  xor         eax,eax 
010D1002  ret
Run Code Online (Sandbox Code Playgroud)

但如果我写"volatile"这个词:

int main()
{
01261000  push        ecx  
    volatile int i;
    i = 1;
01261001  mov         dword ptr [esp],1 
    i = 2;
01261008  mov         dword ptr [esp],2 
}
0126100F  xor         eax,eax 
01261011  pop         ecx  
01261012  ret   
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么VS留下这个代码?这有什么副作用吗?它是程序中唯一的代码,为什么优化器不能把它扔掉?

c++ optimization volatile

5
推荐指数
3
解决办法
241
查看次数

每个套件的 boost-test 初始化(非大小写)

我需要初始化一些变量,它们在 BOOST_AUTO_TEST_SUITE 中是“全局的”,因此它们的构造函数将在套件启动时被调用,并且它们的析构函数将在最后一个相应的 BOOST_AUTO_TEST_CASE 完成后被调用

有人知道我该怎么做吗?看起来全局装置不是解决方案......

c++ fixture suite boost-test

5
推荐指数
2
解决办法
3487
查看次数

C++ 20模块TS:还需要函数声明吗?

我希望向那些尝试过C++ 20模块的人提出一个小问题

根据TS,这个代码应该在C++ 20中编译吗?

void f1() { f2(); }
void f2() { ... }
Run Code Online (Sandbox Code Playgroud)

例如,在C++ 11中它不会编译,因为f1()不"知道"f2(),必须在使用前声明f2().

但也许在C++ 20中,这个要求将在模块中消除?

如果没有编译第一个代码片段,那么这个代码片段是否会编译

void f1() { f2(); }
export void f2() { ... }
Run Code Online (Sandbox Code Playgroud)

因为从BMI可以看到f2()?

c++ module function-declaration c++20

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

constexpr替换宏而不回归

在我们公司的代码中我们使用64位标志枚举:

enum Flags : unsigned long long {
    Flag1 =  1uLL<<0, // 1
    //...
    Flag40 = 1uLL<<40 // 1099511627776
};
Run Code Online (Sandbox Code Playgroud)

并添加注释以查看每个标志十进制值,即使我们在文本查看器中读取代码.问题是没有什么能阻止开发人员在评论中输入错误的数字.

有一个解决这个问题的方法 - 一个带有static_assert +宏的模板可以轻松使用这种方法 - 无需使用括号并在所有地方添加:: val:

template <unsigned long long i, unsigned long long j>
struct SNChecker{
    static_assert(i == j, "Numbers not same!");
    static const unsigned long long val = i;
};

#define SAMENUM(i, j) SNChecker<(i), (j)>::val

enum ET : unsigned long long {
    ET1 =     SAMENUM(1uLL<<2, 4),
    ET2fail = SAMENUM(1uLL<<3, 4), // compile time error
    ET4 = …
Run Code Online (Sandbox Code Playgroud)

c++ macros compile-time constexpr

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

C++ 17类模板参数类型推导 - 可读性

对于支持此功能的编译器,您可以编写

std::tuple t1(1);
Run Code Online (Sandbox Code Playgroud)

但在我看来,这个程序的可读性受到了伤害.

例如,如果你有自己的类与std :: tuple类似,那么读者看不到这个模板类的不同实例可能是不同类型的

Myple t1(1);
Myple t2(1, 2);
Myple t3(1, 3);
...
t1 == t2; // compiler error, t1 and t2 are actually of different types
t2 == t3; // no error
Run Code Online (Sandbox Code Playgroud)

有没有提出如何消除这种"可读性"问题的建议?

我的建议是让程序员写这样的东西

Myple<auto...> t1(1);
Run Code Online (Sandbox Code Playgroud)

(甚至是Myple <...> t1(1);)

所以程序读者至少应该知道,因为他们知道2种不同的汽车可以是不同类型的.

但我相信可以有更智能的解决方案.

编辑

将"用户体验"更改为"可读性".

c++ templates type-deduction c++17

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