小编Pha*_*rap的帖子

我什么时候应该使用std :: size_t?

我只是想知道我应该使用std::size_tfor循环和东西而不是int?例如:

#include <cstdint>

int main()
{
    for (std::size_t i = 0; i < 10; ++i) {
        // std::size_t OK here? Or should I use, say, unsigned int instead?
    }
}
Run Code Online (Sandbox Code Playgroud)

一般来说,何时使用的最佳做法是什么std::size_t

c++ types idiomatic size-t loop-counter

187
推荐指数
9
解决办法
9万
查看次数

static_assert做了什么,你会用它做什么?

你能举个例子static_assert(...)来优雅地解决问题吗?

我熟悉运行时assert(...).我什么时候应该static_assert(...)比常规更喜欢assert(...)

此外,boost有一个叫做的东西BOOST_STATIC_ASSERT,它是一样的static_assert(...)吗?

c++ debugging assert static-assert c++11

110
推荐指数
6
解决办法
5万
查看次数

是一个`= default`移动构造函数,等同于成员移动构造函数吗?

这是

struct Example { 
    int a, b; 
    Example(int mA, int mB) : a{mA}, b{mB}               { }
    Example(const Example& mE) : a{mE.a}, b{mE.b}        { }
    Example(Example&& mE) : a{move(mE.a)}, b{move(mE.b)} { }
    Example& operator=(const Example& mE) { a = mE.a; b = mE.b; return *this; } 
    Example& operator=(Example&& mE)      { a = move(mE.a); b = move(mE.b); return *this; } 
}
Run Code Online (Sandbox Code Playgroud)

相当于此

struct Example { 
    int a, b; 
    Example(int mA, int mB) : a{mA}, b{mB} { }
    Example(const Example& mE)            = default; …
Run Code Online (Sandbox Code Playgroud)

c++ constructor default move-semantics c++11

90
推荐指数
3
解决办法
3万
查看次数

HttpContext.Current.User.Identity.Name如何知道存在哪些用户名?

这不一定是一个问题,我只是好奇它是如何工作的.我有一个方法:

public static bool UserIsAuthenticated()
{
    bool isAuthed = false;
    try
    {
        if (HttpContext.Current.User.Identity.Name != null)
        {
            if (HttpContext.Current.User.Identity.Name.Length != 0)
            {
                FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
                FormsAuthenticationTicket ticket = id.Ticket;
                isAuthed = true;
                string MyUserData = ticket.UserData;
            }
        }
    }
    catch { } // not authed
    return isAuthed;
}
Run Code Online (Sandbox Code Playgroud)

HttpContext.Current.User.Identity.Name返回null如果用户不存在,但它是如何知道哪些用户名存在或不存在?

c# asp.net httpcontext

34
推荐指数
4
解决办法
11万
查看次数

除了OOP之外,什么使C++比C更好

嗯,这可能听起来像一个巨魔问题,但由于C++似乎很难完全掌握(我从来没有真正知道STL实际上是它的"部分"),我想知道在不依赖时使用C而不是C++有什么缺点在OOP上.

C++有时候会有非常复杂的语法,这在我尝试使用OGRE3D时会让我感到困惑......

c c++

32
推荐指数
5
解决办法
8855
查看次数

C++ 17类模板部分演绎

我对类模板提议的模板参数推导的理解是在演绎上下文中使模板函数和模板类的行为同质化.但我认为我误解了一些事情.

如果我们有这个模板对象:

template <std::size_t S, typename T>
struct test
{
    static constexpr auto size = S;
    using type_t = T;

    test(type_t (&input)[size]) : data(input) {}
    type_t (&data)[size]{};
};
Run Code Online (Sandbox Code Playgroud)

我倾向于使用辅助函数作为创建对象的语法糖test:

template <std::size_t S, typename T>
test<S, T> helper(T (&input)[S]) { return input; }
Run Code Online (Sandbox Code Playgroud)

可以使用如下所示:

int main()
{
    int buffer[5];

    auto a = helper<5, int>(buffer); // No deduction
    auto b = helper<5>(buffer);      // Type deduced
    auto c = helper(buffer);         // Type and size deduced

    std::cout …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++17

25
推荐指数
2
解决办法
1413
查看次数

如何在C++中使函数异步?

我想调用一个异步的函数(我将在完成此任务时给出一个回调).

我想在单线程中执行此操作.

c++ asynchronous

20
推荐指数
3
解决办法
3万
查看次数

CPU上奇偶校验标志的用途是什么?

某些CPU(特别是x86 CPU)在其状态寄存器中具有奇偶校验标志.该标志指示操作结果的位数是奇数还是偶数.

奇偶校验标志在编程环境中起什么实际用途?

旁注: 我假设它打算与奇偶校验位一起使用以执行基本的错误检查,但是这样的任务似乎并不常见,以保证整个CPU标志.

assembly cpu-architecture parity eflags

17
推荐指数
3
解决办法
7066
查看次数

如何在睡眠时唤醒std :: thread

我正在使用C++ 11,我有一个std::thread类成员,它每2分钟向听众发送一次信息.其他它只是睡觉.所以,我让它睡了2分钟,然后发送所需的信息,然后再睡2分钟.

// MyClass.hpp
class MyClass {

    ~MyClass();
    RunMyThread();

private:
    std::thread my_thread;
    std::atomic<bool> m_running;
}


MyClass::RunMyThread() {

    my_thread = std::thread { [this, m_running] {
    m_running = true;
    while(m_running) {
        std::this_thread::sleep_for(std::chrono::minutes(2));
        SendStatusInfo(some_info);
    }
}};
}

// Destructor
~MyClass::MyClass() {
    m_running = false; // this wont work as the thread is sleeping. How to exit thread here?
}
Run Code Online (Sandbox Code Playgroud)

问题:
这种方法的问题是我无法在线程休眠时退出线程.我从阅读中理解,我可以使用a唤醒它std::condition_variable并优雅地退出?但我正在努力寻找一个简单的例子,它可以满足上述场景中的要求.condition_variable我发现的所有例子看起来都太复杂了,我想在这里做些什么.

问题:
如何std::condition_variable在睡眠时使用a 唤醒线程并正常退出?或者,没有这种condition_variable技术,还有其他方法可以实现相同的目标吗?

另外,我看到我需要std::mutex结合使用std::condition_variable?这真的有必要吗?通过将std::condition_variable逻辑仅添加到代码中的所需位置,是否无法实现目标? …

c++ mutex condition-variable c++11 stdthread

14
推荐指数
4
解决办法
3663
查看次数

如果只有一个成员没有一个,那么为什么联合会有一个删除的默认构造函数呢?

N3797::9.5/2 [class.union] 说:

如果union的任何非静态数据成员具有非平凡的默认构造函数(12.1),复制构造函数(12.8),移动构造函数(12.8),复制赋值运算符(12.8),移动赋值运算符(12.8)或析构函数( 12.4),联合的相应成员函数必须由用户提供,否则将被隐式删除(8.4.3)

我试图通过例子来理解这个说明:

#include <iostream>
#include <limits>

struct A
{
    A(const A&){ std::cout << "~A()" << std::endl; } //A has no default constructor
};

union U
{
    A a;
};

U u; //error: call to implicitly-deleted default constructor of 'U'

int main()
{

}
Run Code Online (Sandbox Code Playgroud)

DEMO

这种行为对我来说并不十分清楚.struct A没有隐式声明的默认构造函数,因为12.1/4: [class.ctor]说:

如果类X没有用户声明的构造函数,则没有参数的构造函数被隐式声明为默认值(8.4).

这意味着struct A没有非平凡的默认构造函数(根本没有默认构造函数,特别是非平凡的).那union U不必有一个删除的默认构造函数.怎么了?

c++ constructor unions

13
推荐指数
2
解决办法
8473
查看次数