c++ - Schwarz 计数器和可变参数模板

标签 c++ c++11 variadic-templates

没有听说过“Schwarz Counter”的 friend 请引用this维基百科页面。

我正在尝试使用 C++11 模板实现这个习惯用法,一切正常,除非模板参数不是默认可构造的。这是我的实现:(请参阅问题的代码注释)

#include <type_traits>

template<typename T>
class Single {
public:
    static T& get() {
        return reinterpret_cast<T&>(instance.storage);
    }

private:
    struct Instance {
        enum { kSize = sizeof(T), kAlign = std::alignment_of<T>::value };

        int counter;
        typename std::aligned_storage<kSize, kAlign>::type storage;

        template<typename... Arg>
        Instance(Arg&&... param) {
            if (counter++ == 0) {
                new (&storage) T(param...);
            }
        }

        ~Instance() {
            if (--counter == 0) {
                reinterpret_cast<T&>(storage).~T();
            }
        }
    };

    static Instance instance;
};

// The following won't compile if T isn't default constructible.
// I want to get rid of the definition in this case, and let the
// users of this template to provide their own definitions.
// I'd like to keep the it when T is default constructible, because
// most of the time T is (according to my experience, most global 
// objects are constructed without arguments, singletons are
// essentially global objects).
//
// So, basically I want something like this:
// #if std::is_default_constructible<T>::value
//     template<typename T>
//     ...;
// #endif
// Could this be done in C++11 ? If not, how about C++14 ?
template<typename T>
typename Single<T>::Instance Single<T>::instance;

顺便说一句,如果该类是默认可构造的,则使用此模板定义单例非常简单,例如:

class Y {
    friend class Single<Y>;
    Y(int, int) { std::cout << "Y()\n"; }
    ~Y() { std::cout << "~Y()\n"; }
};

class X {
    friend class Single<X>;
    X() { std::cout << "X()\n"; }
    ~X() { std::cout << "~X()\n"; }
};

int main() {
    auto& x = Single<X>::get();
    // auto& g = Single<Y>::get(); // oops
    return 0;
}

最佳答案

创建两个类。一个用于默认可构造 T,另一个用于不可构造。

调度您的主要实现以仅使用默认可构造实例,前提是它是默认可构造的。

template<class T,class=void>
struct WhichSingle {
  using type=Single<T>;
};
template<class T>
struct WhichSingle<T,typename std::enable_if<
  !std::is_default_constructible<T>::value
>::type> {
  using type=SingleNoDefault<T>;
};

template<class X>
using Bob = typename WhichSignle<X>::type;

Bob<X>SingleSingleNoDefault .

更好的名称和减少重复的工作可以完成,但这只是繁重的工作。

关于c++ - Schwarz 计数器和可变参数模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30443262/

相关文章:

c++ - 我正在使用可变参数模板在 C++11 中创建一个元组类。我如何使用它的实例变量?

c++ - 模板参数包什么时候被推导为空?

c++ - 好友功能-成员(member)无法访问

c++ - Arduino 中的预定义数组

C++11 通过推导类型参数传递成员函数调用?

c++ - 将带有所有参数的成员函数传递给 std::function

c++ - 在 C++11 中存储对方法的引用时出错

C++ Evil Hangman 实现问题

c++ - QT4.8 - 对 QLineEdit 实现突出显示

c++ - 参数包扩展的编译时反转