简体   繁体   English

具有std :: enable_if和std :: decay的C ++类构造函数模板

[英]c++ class constructor template with std::enable_if and std::decay

class DirectoryEntry; // forward declaration

template <class T>
struct isPathable { static const bool value = false; };

template<> struct isPathable<char*>
{
    static const bool value = true;
};
template<> struct isPathable<const char*>
{
    static const bool value = true;
};
template<> struct isPathable<std::string>
{
    static const bool value = true;
};
template<> struct isPathable<std::vector<char> >
{
    static const bool value = true;
};
template<> struct isPathable<std::list<char> >
{
    static const bool value = true;
};
template<> struct isPathable<DirectoryEntry>
{
    static const bool value = true;
};

class path
{
private:
    std::string m_pathname;
public:

    // constructors:
    // ------------------------
    path() noexcept {}
    path(const path &p) : m_pathname(p.m_pathname) {}

    template <class Source>
    path(Source const &source,
        std::enable_if_t<isPathable<std::decay_t<Source>> >* = 0)
    {
        // do stuff
    }
...
};

I get the following error message: 我收到以下错误消息:

/usr/bin/c++   -I../lib -Wall -Werror -std=c++17 -g   -pthread -MD -MT app/CMakeFiles/infinityApp.dir/src/main.cpp.o -MF app/CMakeFiles/infinityApp.dir/src/main.cpp.o.d -o app/CMakeFiles/infinityApp.dir/src/main.cpp.o -c ../app/src/main.cpp

error: type/value mismatch at argument 1 in template parameter list for ‘template<bool _Cond, class _Tp> using enable_if_t = typename std::enable_if::type’

std::enable_if_t<isPathable<std::decay_t<Source>> >* = 0)
                                                  ^
note:   expected a constant of type ‘bool’, got ‘isPathable<typename std::decay<_Tp>::type>’

From the error message I see that there is a problem with the isPathable part as it doesn't pass on a bool, but I don't understand the why. 从错误消息中,我看到isPathable部分存在问题,因为它没有通过bool,但是我不明白为什么。 Where is the problem and how should I change my code? 问题出在哪里,我该如何更改代码? Maybe there is a better solution to such problems? 也许对这些问题有更好的解决方案?

template<> struct isPathable<char*>
{
    static const bool value = true;
};

You're defining a bunch of specializations in this fashion. 您正在以这种方式定义一堆专业化知识。 Your specializations define a boolean member value , initialized as true . 您的专业化定义了一个布尔成员value ,并初始化为true In your constructor: 在您的构造函数中:

/* ... */ std::enable_if_t<isPathable<std::decay_t<Source>> >* = 0)

Note that the template parameter to std::enable_if_t is a boolean value, but if you parse out what you're specifying here, you're specifying a typename as a template parameter. 需要注意的是模板参数std::enable_if_t是一个布尔值,但如果你分析出你指定什么在这里,你指定一个typename作为模板参数。 You obviously meant something along the line of... 您显然是在说...

/* ... */ std::enable_if_t<isPathable<std::decay_t<Source>>::value >* = 0)

A few other tweaks you can try and improve your template: 您可以尝试其他一些调整来改进模板:

  • Define your class member as a constexpr , and not merely a const . 将您的类成员定义为constexpr ,而不仅仅是const

  • You can probably avoid using a dummy formal parameter to the constructor, by doing something like: 您可以通过执行以下操作来避免在构造函数中使用虚拟形式参数:

     template <class Source, std::enable_if_t<isPathable<std::decay_t<Source>>::value >> path(Source const &source) { // do stuff } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM