C++ constexpr如何推导该值?

C++ constexpr如何推导该值?,c++,c++11,constexpr,C++,C++11,Constexpr,我正在浏览cppreference.com中提到的文字类型程序。 () 我知道constexpr会在编译时推断值。 但在下面的例子中,第10、12和16行并不直接知道输入参数。(至少我看不出来) 那么它是如何推导出这个值的呢 1 #include <iostream> 2 #include <stdexcept> 3 4 class conststr 5 { 6 const char* p; 7 std::size_t sz;

我正在浏览cppreference.com中提到的文字类型程序。 ()

我知道constexpr会在编译时推断值。 但在下面的例子中,第10、12和16行并不直接知道输入参数。(至少我看不出来)

那么它是如何推导出这个值的呢

  1 #include <iostream>
  2 #include <stdexcept>
  3 
  4 class conststr
  5 {
  6     const char* p;
  7     std::size_t sz;
  8 public:
  9     template<std::size_t N>
 10     constexpr conststr(const char(&a)[N]) : p(a), sz(N - 1) {}
 11 
 12     constexpr char operator[](std::size_t n) const
 13     {
 14         return n < sz ? p[n] : throw std::out_of_range("");
 15     }
 16     constexpr std::size_t size() const { return sz; }
 17 };
 18 
 19 constexpr std::size_t countlower(conststr s, std::size_t n = 0,
 20                                              std::size_t c = 0)
 21 {
 22     return n == s.size() ? c :
 23            s[n] >= 'a' && s[n] <= 'z' ? countlower(s, n + 1, c + 1) :
 24                                         countlower(s, n + 1, c);
 25 }
 26 
 27 // output function that requires a compile-time constant, for testing
 28 template<int n>
 29 struct constN
 30 {
 31     constN() { std::cout << n << '\n'; }
 32 };
 33 
 34 int main()
 35 {
 36     std::cout << "the number of lowercase letters in \"Hello, world!\" is ";
 37     constN<countlower("Hello, world!")>(); // implicitly converted to conststr
 38 }
1#包括
2#包括
3.
4类constr
5 {
6常量字符*p;
7标准:尺寸大小;
8公众:
9模板
10 constexpr conststr(constchar(&a)[N]):p(a),sz(N-1){
11
12 constexpr字符运算符[](标准::大小\u t n)const
13     {
14返回n23 s[n]>='a'&&s[n]当到达第37行
constN();
时,编译器会尝试推导该值并将其替换到位

所以编译器调用函数
countlower(“Hello,world!”)
。参数
std::size\u t n=0,std::size\u t c=0
然后设置为默认值,因为它们没有传入


函数体由递归
返回n==s.size()?c:s[n]>='a'和&s[n]组成注意:删除了我的答案,因为它虽然正确,但与您的代码无关。好的。这是非常详细的。谢谢。我现在的疑问是,当函数
countlower
接收到第一个参数
conststr s
时,它构造了
conststr
的对象。用外行的话来说可能是这样的
conststr s=“你好,世界!”
(不确定,但假设)。当你看到
conststr
的构造函数时,它有一个模板参数
N
,这是如何用什么值计算出来的?@SSHegde我在最后一段中添加了一些构造函数和模板参数的解释。明白你的意思。理解
constN
的情况。再次感谢。但不知何故,我无法理解bel下面的场景:当构建
conststr
对象时,
N
constchar(&a)[N]
中包含什么值?(非常抱歉拖了这么久)@当我试图解释从
char数组到
conststr的转换时,SSHegde添加了一个
隐式转换
部分。这消除了所有的疑问。非常感谢。现在是在公园里散步:)
struct constN
{
    int n;

    // constructor for the object taking one argument
    constN(int n) : n(n) {};
}
struct CasualObject
{
    int n;

    CasualObject(int n) : n(n) {};
}

template<int n>
struct YourObject
{
    YourObject() { std::cout << n << '\n'; }
};

CasualconstN(9) == YourconstN<9>()
int a = countlower("Hey");
constN obj1(a);

constN obj2(countlower("Hey"));

obj1 == obj2;
#include <iostream>

class conststr
{
    const char* p;
    std::size_t sz;
public:
    template<std::size_t N>
    constexpr conststr(const char(&a)[N]) : p(a), sz(N - 1) {}
    
    constexpr std::size_t size() const { return sz; }
};

int main()
{
    char a[4] = "Hey";
    const char b[4] = "Hey";
    
    conststr x(a);
    conststr y(b);
    conststr z("Hey");
    
    printf("%lu %lu %lu", x.size(), y.size(), z.size());
    return 0;
}
char a[4] = "Hey";
const char b[4] = "Hey";
conststr x(a);
conststr(const char(&a))
// which can be converted to all of these:
conststr(const char a[])
conststr(char* a)
conststr(char (&a))
template<std::size_t N>
conststr(const char(&a)[N])
// so from main() we have:
char a[4] = "Hey";
// this can be rewritten like so:
const char a[4] = "Hey";
    
// now it looks very similar to the definition of the constructor:
const char(&a)[N]
const char a[4]
conststr(const char(&a)[N]) : p(a),  sz(N - 1)
conststr(const char a[4]): p("Hey"), sz(4 - 1)
conststr z("Hey");