使用std :: enable_if<>进行模板专业化

时间:2015-06-14 15:13:21

标签: c++ templates c++11 template-specialization

以下代码编译并运行:

#include <cinttypes>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <sstream>
#include <stdexcept>

class UnsignedBox {
public:
    typedef std::uint64_t box_type;

    template<typename UNSIGNED_TYPE, 
        typename std::enable_if<
        std::numeric_limits<UNSIGNED_TYPE>::is_signed==false &&
        (sizeof(UNSIGNED_TYPE) >= sizeof(UnsignedBox::box_type)), int>::type = 0 
    >
    UNSIGNED_TYPE toUnsigned()const {
        //We've established we're not returning a smaller type so we can just 
       //return our value.
        return value;
    }

    template<typename UNSIGNED_TYPE, 
       typename std::enable_if<std::numeric_limits<UNSIGNED_TYPE>::is_signed==false &&
       (sizeof(UNSIGNED_TYPE) < sizeof(UnsignedBox::box_type)), int>::type = 0
    >
    UNSIGNED_TYPE toUnsigned()const {
        //We are returning  a smaller type so we need a range check.
        if(value>static_cast<box_type>(std::numeric_limits<UNSIGNED_TYPE>::max())){
            std::ostringstream msg;
            msg<<value<<'>'<<
               static_cast<box_type>(std::numeric_limits<UNSIGNED_TYPE>::max());
            throw std::logic_error(msg.str());
        }
        return value;
    }

    UnsignedBox(const box_type ivalue): value(ivalue){}
private:
    box_type value;

};

int main(int argc, char*argv[]) {

    UnsignedBox box(
        static_cast<UnsignedBox::box_type>(    
           std::numeric_limits<std::uint32_t>::max())+10
        );

    std::uint64_t v(box.toUnsigned<std::uint64_t>());
    std::cout<<v<<std::endl;

    try {
        std::uint32_t v(box.toUnsigned<std::uint32_t>());
    }catch(const std::logic_error err){
        std::cout<<err.what()<<std::endl;
    }

    return EXIT_SUCCESS;
}

预期产出(所有支持平台):

4294967305
4294967305>4294967295

到目前为止一切顺利。

但我真正想做的事情(代码清晰度):

声明如下:

template<typename UNSIGNED_TYPE>
UNSIGNED_TYPE toUnsigned()const;

然后提供专门的实现,例如:

template<typename UNSIGNED_TYPE, 
        typename std::enable_if<
        std::numeric_limits<UNSIGNED_TYPE>::is_signed==false &&
        (sizeof(UNSIGNED_TYPE) >= sizeof(UnsignedBox::box_type)), int>::type = 0 
    >
    UNSIGNED_TYPE UnsignedBox::toUnsigned()const {
        //We've established we're not returning a smaller type so we can just 
       //return our value.
        return value;
    }


    template<typename UNSIGNED_TYPE, 
       typename std::enable_if<std::numeric_limits<UNSIGNED_TYPE>::is_signed==false &&
       (sizeof(UNSIGNED_TYPE) < sizeof(UnsignedBox::box_type)), int>::type = 0
    >
    UNSIGNED_TYPE UnsignedBox::toUnsigned()const {
        //We are returning  a smaller type so we need a range check.
        if(value>static_cast<box_type>(std::numeric_limits<UNSIGNED_TYPE>::max())){
            std::ostringstream msg;
            msg<<value<<'>'<<
               static_cast<box_type>(std::numeric_limits<UNSIGNED_TYPE>::max());
            throw std::logic_error(msg.str());
        }
        return value;
    }

但是我收到了这个错误:

xxx.cpp:nn:20: error: prototype for 'UNSIGNED_TYPE UnsignedBox::toUnsigned() const' does not match any in class 'UnsignedBox'
      UNSIGNED_TYPE UnsignedBox::toUnsigned()const {
                    ^ xxx.cpp:nn:23: error: candidate is: template<class UNSIGNED_TYPE> UNSIGNED_TYPE UnsignedBox::toUnsigned() const
         UNSIGNED_TYPE toUnsigned()const;
                       ^

这很奇怪,因为如果你问我原型

UNSIGNED_TYPE UnsignedBox::toUnsigned() const

非常适合

UNSIGNED_TYPE toUnsigned()const;

我做错了什么?

PS:这不是实际问题,但我的问题是类似的,我想根据编译时检查的基元类型的属性特殊一些模板。

2 个答案:

答案 0 :(得分:3)

您无法使用一个签名声明一个函数:

template<typename UNSIGNED_TYPE>
UNSIGNED_TYPE toUnsigned() const;

然后用不同的签名定义它:

template<typename UNSIGNED_TYPE, 
    typename std::enable_if<
    std::numeric_limits<UNSIGNED_TYPE>::is_signed==false &&
    (sizeof(UNSIGNED_TYPE) >= sizeof(UnsignedBox::box_type)), int>::type = 0 
>
UNSIGNED_TYPE UnsignedBox::toUnsigned() const;

第一个采用一个模板参数,第二个采用两个 - 即使一个是默认的。两者必须完全匹配。所以你需要两个声明:

template <typename UNSIGNED_TYPE,
          typename = typename std::enable_if<
                        std::is_unsigned<UNSIGNED_TYPE>::value &&
                        sizeof(UNSIGNED_TYPE) >= sizeof(UnsignedBox::box_type)
                        >::type>
UNSIGNED_TYPE toUnsigned() const;

template <typename UNSIGNED_TYPE,
          typename = typename std::enable_if<
                        std::is_unsigned<UNSIGNED_TYPE>::value &&
                        sizeof(UNSIGNED_TYPE) < sizeof(UnsignedBox::box_type)
                        >::type>
UNSIGNED_TYPE toUnsigned() const;

然后是两个定义。此外,这本身并不起作用,因为我们有效地重新定义了默认模板参数,因此您需要在返回类型上使用SFINAE,例如:

template <typename UNSIGNED_TYPE>
typename std::enable_if<
    std::is_unsigned<UNSIGNED_TYPE>::value &&
    sizeof(UNSIGNED_TYPE) >= sizeof(UnsignedBox::box_type),
    UNSIGNED_TYPE>::type
toUnsigned() const;


template <typename UNSIGNED_TYPE>
typename std::enable_if<
    std::is_unsigned<UNSIGNED_TYPE>::value &&
    sizeof(UNSIGNED_TYPE) < sizeof(UnsignedBox::box_type),
    UNSIGNED_TYPE>::type
toUnsigned() const;

虽然将toUnsigned()转发给基于sizeof的其他两个成员函数可能更简单:

template <typename UNSIGNED_TYPE,
          typename = typename std::enable_if<std::is_unsigned<UNSIGNED_TYPE>::value>::type>
UNSIGNED_TYPE toUnsigned() const {
    return toUnsigned<UNSIGNED_TYPE>(
        std::integral_constant<bool, 
            (sizeof(UNSIGNED_TYPE) >= sizeof(UnsignedBox::box_type))>{});
}

template <typename UNSIGNED_TYPE>
UNSIGNED_TYPE toUnsigned(std::true_type /* bigger */);

template <typename UNSIGNED_TYPE>
UNSIGNED_TYPE toUnsigned(std::false_type /* smaller */);

答案 1 :(得分:0)

根据Barry(上图)的一些见解,我已经确定了我的回答:

#include <cinttypes>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <sstream>
#include <stdexcept>


//Here is the real aim - a short and sweet class declaration pretty much free
//of implementation junk and jiggery-pokery.

class UnsignedBox {
public:
    typedef std::uint64_t box_type;

    template<typename UNSIGNED_TYPE> UNSIGNED_TYPE toUnsigned()const;

    UnsignedBox(const box_type ivalue): value(ivalue){}
private:
    box_type value;
};

//Now things get a bit more verbose...

namespace UnsignedBox_support {

    template<
        typename FROM_TYPE,
        typename TO_TYPE,
        bool IS_UNSIGNED=(std::numeric_limits<TO_TYPE>::is_signed==false),
        bool FROM_IS_LARGER=(sizeof(FROM_TYPE)>sizeof(TO_TYPE))
    >
    class ToUnsigned{ };

    template<typename FROM_TYPE,typename TO_TYPE>
    class ToUnsigned<FROM_TYPE,TO_TYPE,true,false>{
        template<typename UNSIGNED_TYPE> 
            friend UNSIGNED_TYPE UnsignedBox::toUnsigned()const;

        static TO_TYPE convert(const FROM_TYPE v){
            //No checking...
            return static_cast<TO_TYPE>(v);
        }
    };

    template<typename FROM_TYPE,typename TO_TYPE>
    class ToUnsigned<FROM_TYPE,TO_TYPE,true,true>{
        template<typename UNSIGNED_TYPE> 
            friend UNSIGNED_TYPE UnsignedBox::toUnsigned()const;

        static TO_TYPE convert(const FROM_TYPE v){
            if(v>static_cast<FROM_TYPE>(std::numeric_limits<TO_TYPE>::max())){
                std::ostringstream msg;
                msg<<v<<'>'<<
                    static_cast<FROM_TYPE>(std::numeric_limits<TO_TYPE>::max());
                throw std::logic_error(msg.str());
            }             
            return static_cast<TO_TYPE>(v);
        }
    };
}

template<typename UNSIGNED_TYPE> UNSIGNED_TYPE UnsignedBox::toUnsigned()const{
    return UnsignedBox_support::ToUnsigned<
        UnsignedBox::box_type,UNSIGNED_TYPE
    >::convert(this->value);    
    //TEMPLATE USE DEBUGGING:
    //If you find yourself here being told ToUnsigned has no member
    //convert() then it's possible you're trying to implement 
    //this member for a signed data-type.
}

int main(int argc, char*argv[]) {
    UnsignedBox box(
        static_cast<UnsignedBox::box_type>(std::numeric_limits<std::uint32_t>::max())+10
    );
    std::uint64_t v(box.toUnsigned<std::uint64_t>());
    std::cout<<v<<std::endl;

    try {
        std::uint32_t v(box.toUnsigned<std::uint32_t>());
    }catch(const std::logic_error err){
        std::cout<<err.what()<<std::endl;
    }
    return EXIT_SUCCESS;
}

诀窍是实现一个成员函数(不能部分专门化)并让它调用一个类(可以部分专门化。

请注意,未在支持类convert()中声明成员ToUnsigned滥用模板并尝试将其调用为签名类型会引发编译错误。否则,您将面临更难追踪链接错误的风险。 如果您向main()添加了这样的行,我已经添加了评论,以表明您可能会被采纳:

int xxx(box.toUnsigned<int>());

我不得不说我认为这不是一个丑陋的黑客,而不是任何std::enable_if<>解决方案,并通过使支持成员私有和friend成员,他们在那里帮助实现它至少一些努力工作以封装&#39;。

它还为进一步专业化打开了大门,补充或压倒已经给予的专业。我并不是说所有模板都应该写成允许进一步专业化,但我认为开门是有用的。

相关问题