Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在通过模板元编程计算C样式字符串的长度时,必须指定上限 我最近开始在C++中使用模板元编程,并试图评估C风格字符串的长度。_C++_Template Meta Programming - Fatal编程技术网

在通过模板元编程计算C样式字符串的长度时,必须指定上限 我最近开始在C++中使用模板元编程,并试图评估C风格字符串的长度。

在通过模板元编程计算C样式字符串的长度时,必须指定上限 我最近开始在C++中使用模板元编程,并试图评估C风格字符串的长度。,c++,template-meta-programming,C++,Template Meta Programming,我已经成功地使用了这段代码 template <const char *str, std::size_t index> class str_length { public: static inline std::size_t val() { return (str[index] != '\0') ? (1 + str_length<str, index + 1>::val()) : 0; } }; template

我已经成功地使用了这段代码

template <const char *str, std::size_t index>
class str_length {
public:
    static inline std::size_t val() {
        return (str[index] != '\0') ? (1 + str_length<str, index + 1>::val()) :             0;
    }
};

template <const char *str>
class str_length <str, 500> {
public:
    static inline std::size_t val() {
        return 0;
    }
};

extern const char bitarr[] { "0000000000000000000" };

int main() {
    std::cout << str_length<bitarr, 0>::val() << std::endl;

    getchar();
    return 0;
}
模板
类stru长度{
公众:
静态内联std::size\u t val(){
返回(str[index]!='\0')?(1+str_length::val()):0;
}
};
模板
类stru长度{
公众:
静态内联std::size\u t val(){
返回0;
}
};
外部常量字符位数组[]{“0000000000000000000”};
int main(){

std::cout在这种情况下,停止无限模板实例化的常用方法是使用专门化;专门化与
constepr
-任何事物的性质都是正交的。查看C++14中扩展的
constepr
允许的附加内容列表,我在下面的示例中没有看到需要扩展的
constepr支持。gcc 6.1.1在符合性模式下编译此代码,FWIW:

#include <iostream>

template<const char *str, size_t index, char c> class str_length_helper;

template <const char *str>
class str_length {
public:

    static constexpr std::size_t val()
    {
        return str_length_helper<str, 0, str[0]>::val();
    }
};

template<const char *str, std::size_t index, char c>
class str_length_helper {

public:

    static constexpr std::size_t val()
    {
        return 1+str_length_helper<str, index+1, str[index+1]>::val();
    }
};

template<const char *str, std::size_t index>
class str_length_helper<str, index, 0> {
public:

    static constexpr std::size_t val()
    {
        return 0;
    }
};

static constexpr char bitarr[] { "0000000000000000000" };

int main() {
    std::cout << str_length<bitarr>::val() << std::endl;

    getchar();
    return 0;
}
#包括
模板类str_length_helper;
模板
类stru长度{
公众:
静态constexpr std::size\u t val()
{
返回str_length_helper::val();
}
};
模板
类str_length_helper{
公众:
静态constexpr std::size\u t val()
{
返回1+str_length_helper::val();
}
};
模板
类str_length_helper{
公众:
静态constexpr std::size\u t val()
{
返回0;
}
};
静态constexpr char bitarr[]{“0000000000000000000”};
int main(){

std::cout在这种情况下,停止无限模板实例化的常用方法是使用专门化;专门化与
constepr
-任何事物的性质都是正交的。查看C++14中扩展的
constepr
允许的附加内容列表,我在下面的示例中没有看到需要扩展的
constepr支持。gcc 6.1.1在符合性模式下编译此代码,FWIW:

#include <iostream>

template<const char *str, size_t index, char c> class str_length_helper;

template <const char *str>
class str_length {
public:

    static constexpr std::size_t val()
    {
        return str_length_helper<str, 0, str[0]>::val();
    }
};

template<const char *str, std::size_t index, char c>
class str_length_helper {

public:

    static constexpr std::size_t val()
    {
        return 1+str_length_helper<str, index+1, str[index+1]>::val();
    }
};

template<const char *str, std::size_t index>
class str_length_helper<str, index, 0> {
public:

    static constexpr std::size_t val()
    {
        return 0;
    }
};

static constexpr char bitarr[] { "0000000000000000000" };

int main() {
    std::cout << str_length<bitarr>::val() << std::endl;

    getchar();
    return 0;
}
#包括
模板类str_length_helper;
模板
类stru长度{
公众:
静态constexpr std::size\u t val()
{
返回str_length_helper::val();
}
};
模板
类str_length_helper{
公众:
静态constexpr std::size\u t val()
{
返回1+str_length_helper::val();
}
};
模板
类str_length_helper{
公众:
静态constexpr std::size\u t val()
{
返回0;
}
};
静态constexpr char bitarr[]{“0000000000000000000”};
int main(){

std::cout为什么不使用
bitarr
是一个数组,因此已经有了编译时大小这一事实?正如我所说,只是玩弄TMP。@Nicolas数组末尾之前可能有“\0”。不太可能,但可能。为什么不使用
bitarr
是一个数组,因此已经有一个compi时间大小?正如我所说,只是在玩TMP。@Nicolas数组末尾之前可能有“\0”。不太可能,但可能。感谢您的快速回复。很遗憾,这在VC++2015上似乎不起作用。收到此错误()嗯,看起来您的编译器也没有完全的C++11支持。感谢您的快速回复。不幸的是,这在VC++2015上似乎不起作用。收到此错误()嗯,看起来您的编译器也没有完全的C++11支持。