小编sim*_*mon的帖子

模板类实现比较运算符

我将所有重载的比较运算符写入类是一个常见的任务,所以我编写了一个模板类,如果派生类实现了==和<,则实现<,<=,> =,!=.它工作但有很多演员和不那么明显的"奇怪的重复模板模式",所以我想知道是否有更简单的解决方案?

template <class Derived>
class Comparable
{
public:

    bool operator!=(const Comparable<Derived>& other) {
        return !(static_cast<Derived*>(this)->operator==
                     (*static_cast<const Derived*>(&other)));
    }

    bool operator<=(const Comparable<Derived>& other) {
        return (static_cast<Derived*>(this)->operator==
                    (*static_cast<const Derived*>(&other)))
                || (static_cast<Derived*>(this)->operator<
                    (*static_cast<const Derived*>(&other)));
    }

    bool operator>(const Comparable<Derived>& other) {
        return !(static_cast<Derived*>(this)->operator==
                    (*static_cast<const Derived*>(&other)))
                && !(static_cast<Derived*>(this)->operator<
                    (*static_cast<const Derived*>(&other)));
    }

    bool operator>=(const Comparable<Derived>& other) {
        return !(static_cast<Derived*>(this)->operator<
                    (*static_cast<const Derived*>(&other)));
    }
};
Run Code Online (Sandbox Code Playgroud)

c++ templates operator-overloading

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

decltype(*this)等效的外部函数体

是否可以编写一个特征,这会产生它所使用的类的类型?如何在下面的例子中实现get_class?

class Foo {
    typedef get_class::type type; // type = Foo now
}; 
Run Code Online (Sandbox Code Playgroud)

注意:我必须编写一个宏,它在类体中扩展,用于多个类,所以我不能简单地写'typedef Foo type;'

使用案例:

我有一个可反射的(...)宏,它生成基础设施来迭代成员,访问它们并使用它们的名称查找它们:

class Foo 
{
    friend std::ostream &operator<<(std::ostream &, const Foo&);
    reflectable(
         (int) bar,
         (double) baz
    )
}
Run Code Online (Sandbox Code Playgroud)

reflectable(...)应该是一个宏,所以我可以将类型和成员名称分别作为字符串来为查找构建映射.

我希望所有可反射的类都是可流式的,但如果我将reflectable()宏放到私有部分,我必须将友元声明添加到类中.我想将它移动到宏:

friend std::ostream &operator<<(std::ostream &, const get_class::type&);
Run Code Online (Sandbox Code Playgroud)

c++ type-traits c++11

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

在stdin阻塞Windows时退出应用程序

我有一个应用程序,它使用线程中的getline()从标准输入读取数据.我想从主线程关闭应用程序,而getline仍然阻止其他线程.怎么能实现这一目标?

我不想强迫用户必须按ctrl-Z来关闭stdin和应用程序.

到目前为止,我已尝试使用Windows 8.1 64bit,v120平台工具集上的编译器设置(RuntimeLibrary =/MT):

  • freopen stdin,但它被内部锁定阻止
  • 破坏线程,调用abort()
  • 将一个Eof,行结尾放到std :: cin,它也被阻止了

*更新*

  • detach()不起作用,exit()被锁定阻止
  • winapi TerminatThread()调用abort()
  • winapi CloseHandle(GetStdHandle(STD_INPUT_HANDLE))挂起
  • 调用TerminateProcess() - 有效,但我想优雅地退出

*更新2:解决方案*

  • WriteConsoleInput()可以使std :: getline()从阻塞读取返回.这适用于任何msvc运行时库.对于工作解决方案的代码,请参见已接受

显示问题的示例代码:

#include <iostream>
#include <thread>
#include <string>
#include <chrono>

int main(int argc, char *argv[])
{
    bool stop = false;
    std::thread *t = new std::thread([&]{
        std::string line;
        while (!stop && std::getline(std::cin, line, '\n')) {
            std::cout << line;
        }
    });

    std::this_thread::sleep_for(std::chrono::seconds(1));

    stop = true;
    // how to stop thread or make getline to return here?

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

windows multithreading stdin c++11 msvc12

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

不同命名空间中的模板特化静态成员

命名空间中有一个模板类

namespace N
{
    template <typename T>
    class Foo {
        static const T bar;
    };
}
Run Code Online (Sandbox Code Playgroud)

并在不同的命名空间中进行专门化:

namespace O
{
    typedef N::Foo<int> Baz;

    template<>
    const int Baz::bar = 1;
}
Run Code Online (Sandbox Code Playgroud)

此代码使用gcc(4.9.2)编译,但无法使用msvc(v120)进行编译:

error C2888: 'const int N::Foo<int>::bar' : symbol cannot be defined within namespace 'O'
Run Code Online (Sandbox Code Playgroud)

如果我理解正确,代码不符合C++ 11:

应在包含专用模板的命名空间中声明显式特化.其声明者id未限定的显式特化应在模板的最近的封闭命名空间中声明,或者,如果命名空间是内联(7.3.1),则从其封闭的命名空间集中声明任何命名空间.

这是编译器错误还是我误解了?

c++ gcc templates visual-c++ c++11

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

将计时持续时间转换为time_point

如何将计时持续时间转换为time_point,这比给定持续时间的clock's epoch晚?我试图在计时时钟中找到纪元时间而没有成功.

c++11 c++-chrono

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

SFINAE 用于铸造操作员

我有一个类,它包装一个枚举并为其提供字符串转换。现在我引入了模板参数 \'fastStringConvert\',它控制如何使用 SFINAE 进行转换(可在此处找到:how can I use std::enable_if in a conversion operator?)。代码在 MSVC 下编译,但在 GCC 和 Clang 下失败。

\n\n
error: no type named \xe2\x80\x98type\xe2\x80\x99 in \xe2\x80\x98struct std::enable_if<false, void>\xe2\x80\x99\n
Run Code Online (Sandbox Code Playgroud)\n\n

可能是什么问题以及我应该如何更改代码?

\n\n

下面或此处代码的相关部分:http://rextester.com/SYC74124

\n\n
#include <map>\n#include <string>\n#include <type_traits>\n\ntemplate <\n    class SubClass, \n    typename EnumType, \n    bool fastStringConvert = true\n>\nclass SmartEnum\n{\npublic:\n    template <\n        typename SFINAEPostponer = EnumType,\n        typename = typename std::enable_if<fastStringConvert, void>::type\n    >\n    explicit operator const std::string&() const \n    {\n        auto name = SubClass::names().find((int)value);\n        if (name != SubClass::names().end())\n        {\n …
Run Code Online (Sandbox Code Playgroud)

c++ gcc sfinae enable-if typecast-operator

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

方法存在检查器代码在vs2015中断

以下代码检查类A中是否存在foo()方法.此代码在vs2013下编译,但静态断言在vs2015上失败.哪个编译器版本说实话?如果vs2015,那么如何修复代码?

#include <type_traits>

struct MethodTester_foo {
    template<typename U, typename MethodType>
    static auto test(U* p) -> decltype(static_cast<MethodType>(U::foo));
    template<typename U, typename MethodType> static auto test(...)->std::false_type;
};

template <typename Class, typename MethodType, class MethodTester>
using HasMethod =
typename std::conditional
<
    std::is_same<
        decltype(MethodTester::template test<Class, MethodType>(0)),
        std::false_type
    >::value,
    std::false_type, std::true_type
>::type;

struct A { int foo() { return 1; } };

static_assert(HasMethod<A, int(A::*)(), MethodTester_foo>::value, "Has no method named foo");
Run Code Online (Sandbox Code Playgroud)

c++ c++11 msvc12 visual-studio-2015 msvc14

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