是否可以按模板类型更改静态const类成员的值?

时间:2017-06-09 22:30:53

标签: c++ templates

我真的不知道这方面的术语,我只是举个例子:

value_holder<float> floaty;
std::cout << floaty.is_integer << "\n";

这样当我做的时候

    <!DOCTYPE html>
    <html> 
    <head> 
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" id="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
        <title>Full Screen Slider - Jssor Slider</title>
        <style>
            html, body {
                /*position: fixed;*/
                margin: 0;
                padding: 0;
                width: 100%;
                height: 100%;
            }
        </style>
    </head> 
    <body style="font-family: Arial, Verdana, Sans-Serif;">
        <script type="text/javascript" src="../js/jssor.slider-25.0.6.min.js"></script>
        <script>
            jssor_slider1_init = function () {

                var options = {
                    $AutoPlay: 0,                                       //[Optional] Auto play or not, to enable slideshow, this option must be set to greater than 0. Default value is 0. 0: no auto play, 1: continuously, 2: stop at last slide, 4: stop on click, 8: stop on user navigation (by arrow/bullet/thumbnail/drag/arrow key navigation)
                    $Idle: 1500,                                        //[Optional] Interval (in milliseconds) to go for next slide since the previous stopped if the slider is auto playing, default value is 3000
                    $PauseOnHover: 1,                                   //[Optional] Whether to pause when mouse over if a slider is auto playing, 0 no pause, 1 pause for desktop, 2 pause for touch device, 3 pause for desktop and touch device, 4 freeze for desktop, 8 freeze for touch device, 12 freeze for desktop and touch device, default value is 1
                    $DragOrientation: 1,                                //[Optional] Orientation to drag slide, 0 no drag, 1 horizental, 2 vertical, 3 either, default value is 1 (Note that the $DragOrientation should be the same as $PlayOrientation when $Cols is greater than 1, or parking position is not 0)
                    $ArrowKeyNavigation: 1,                             //[Optional] Steps to go for each navigation request by pressing arrow key, default value is 1.
                    $SlideDuration: 800,                                //Specifies default duration (swipe) for slide in milliseconds

                    $ArrowNavigatorOptions: {                           //[Optional] Options to specify and enable arrow navigator or not
                        $Class: $JssorArrowNavigator$,                  //[Requried] Class to create arrow navigator instance
                        $ChanceToShow: 2                                //[Required] 0 Never, 1 Mouse Over, 2 Always
                    }
                };

                var jssor_slider1 = new $JssorSlider$('slider1_container', options);
                //responsive code begin
                //you can remove responsive code if you don't want the slider scales while window resizing
                function ScaleSlider() {
                    var parentNode = jssor_slider1.$Elmt.parentNode;
                    var parentWidth = parentNode.clientWidth;
                    if (parentWidth) {
                        var parentHeight = parentNode.clientHeight;
                        jssor_slider1.$ScaleSize(parentWidth, parentHeight);
                    }
                    else {
                        $Jssor$.$Delay(ScaleSlider, 30);
                    }
                }

                ScaleSlider();

                $Jssor$.$AddEvent(window, "load", ScaleSlider);
                $Jssor$.$AddEvent(window, "resize", ScaleSlider);
                $Jssor$.$AddEvent(window, "orientationchange", ScaleSlider);
                //responsive code end
            };
        </script>

        <!-- Jssor Slider Begin -->
        <div style="position:relative;top:0;left:0;width:100%;height:100%;overflow:hidden;">
            <div id="slider1_container" style="margin: 0 auto; position: relative; top: 0px; left: 0px; width: 800px;
                height: 356px; background: #191919; overflow: hidden;">

                <!-- Loading Screen -->
                <div data-u="loading" style="position:absolute;top:0px;left:0px;background:url('../img/loading.gif') no-repeat 50% 50%; background-color: rgba(0, 0, 0, .7);"></div>

                <!-- Slides Container -->
                <div data-u="slides" style="position: absolute; left: 0px; top: 0px; width: 800px; height: 356px; overflow: hidden;">
                    <div>
                        <img u="image" src="../img/alila/01.jpg" />
                    </div>
                    <div>
                        <img u="image" src="../img/alila/02.jpg" />
                    </div>
                    <div>
                        <img u="image" src="../img/alila/03.jpg" />
                    </div>
                    <div>
                        <img u="image" src="../img/alila/04.jpg" />
                    </div>
                    <div>
                        <img u="image" src="../img/alila/05.jpg" />
                    </div>
                    <div>
                        <img u="image" src="../img/alila/06.jpg" />
                    </div>
                    <div>
                        <img u="image" src="../img/alila/07.jpg" />
                    </div>
                    <div>
                        <img u="image" src="../img/alila/08.jpg" />
                    </div>
                    <div>
                        <img u="image" src="../img/alila/09.jpg" />
                    </div>
                    <div>
                        <img u="image" src="../img/alila/10.jpg" />
                    </div>

                    <div>
                        <img u="image" src="../img/alila/11.jpg" />
                    </div>
                    <div>
                        <img u="image" src="../img/alila/12.jpg" />
                    </div>
                </div>

                <!--#region Arrow Navigator Skin Begin -->
                <style>
                    /* jssor slider arrow navigator skin 05 css */
                    /*
                    .jssora05l                  (normal)
                    .jssora05r                  (normal)
                    .jssora05l:hover            (normal mouseover)
                    .jssora05r:hover            (normal mouseover)
                    .jssora05l.jssora05ldn      (mousedown)
                    .jssora05r.jssora05rdn      (mousedown)
                    .jssora05l.jssora05lds      (disabled)
                    .jssora05r.jssora05rds      (disabled)
                    */
                    .jssora05l, .jssora05r {
                        display: block;
                        position: absolute;
                        /* size of arrow element */
                        width: 40px;
                        height: 40px;
                        cursor: pointer;
                        background: url(../img/a17.png) no-repeat;
                        overflow: hidden;
                    }
                    .jssora05l { background-position: -10px -40px; }
                    .jssora05r { background-position: -70px -40px; }
                    .jssora05l:hover { background-position: -130px -40px; }
                    .jssora05r:hover { background-position: -190px -40px; }
                    .jssora05l.jssora05ldn { background-position: -250px -40px; }
                    .jssora05r.jssora05rdn { background-position: -310px -40px; }

                    .jssora05l.jssora05lds { background-position: -10px -40px; opacity: .3; pointer-events: none; }
                    .jssora05r.jssora05rds { background-position: -70px -40px; opacity: .3; pointer-events: none; }
                </style>
                <!-- Arrow Left -->
                <span u="arrowleft" class="jssora05l" style="top: 158px; left: 8px;">
                </span>
                <!-- Arrow Right -->
                <span u="arrowright" class="jssora05r" style="top: 158px; right: 8px">
                </span>
                <!--#endregion Arrow Navigator Skin End -->

                <!-- Trigger -->
                <script>
                    jssor_slider1_init();
                </script>
            </div>
        </div>
        <!-- Jssor Slider End -->

        <!-- remove the following if no need-->
        <p>other content</p>
    </body> 
    </html>

它会打印0

我如何定义成员is_integer以便这样做?

3 个答案:

答案 0 :(得分:2)

您可以使用std::is_same来做到这一点 它遵循一个最小的工作示例:

#include<type_traits>

template <typename T>
struct value_holder {
    T value;
    static const bool is_integer = std::is_same<int, T>::value or std::is_same<long, T>::value;
};

int main() {
    static_assert(value_holder<int>::is_integer, "!");
    static_assert(not value_holder<char>::is_integer, "!");
}

另一种可能的方法是基于模板专业化。这种方式应该有效:

template <typename T>
struct value_holder {
    T value;
    static const bool is_integer = false;
};

template <>
struct value_holder<int> {
    int value;
    static const bool is_integer = true;
};

template <>
struct value_holder<long> {
    long value;
    static const bool is_integer = true;
};

无论如何,从我的观点来看,它有点冗长,如果你的班级包含多个数据成员,可能会很烦人。

答案 1 :(得分:2)

正如昆汀的回答所说,你使用了一种类型特征。 std::is_integral在您的示例中有意义:

template <typename T>
struct value_holder {
    T value;
    static constexpr bool is_integer = std::is_integral<T>::value;
};

但这并不完全符合您的评论。如果您确实希望is_integer仅对intlong成为真,那​​么您可以定义自定义类型特征:

template <typename T>
struct is_int_or_long : std::false_type {};

template <>
struct is_int_or_long<int> : std::true_type {};

template <>
struct is_int_or_long<long> : std::true_type {};

template <typename T>
struct value_holder {
    T value;
    static constexpr bool is_integer = is_int_or_long<T>::value;
};

当然,使用std::is_same特征可以缩短这一点:

template <typename T>
struct value_holder {
    T value;
    static constexpr bool is_integer = std::is_same<T, int>::value || std::is_same<T, long>::value;
};

答案 2 :(得分:0)

您正在寻找类型特征。您可能会对std::is_integral感兴趣。