Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 714521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:05:04+00:00 2026-05-14T05:05:04+00:00

If I change the type to const char str[Len] , I get the following

  • 0

If I change the type to const char str[Len], I get the following error:

error: no matching function for call to ‘static_strlen(const char [5])’

Am I correct that static_strlen expects an array of const char references? My understanding is that arrays are passed as pointers anyway, so what need is there for the elements to be references? Or is that interpretation completely off-the-mark?

#include <iostream>

template <size_t Len>
size_t
static_strlen(const char (&str)[Len])
{
  return Len - 1;
}

int main() {
  std::cout << static_strlen("oyez") << std::endl;
  return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:05:04+00:00Added an answer on May 14, 2026 at 5:05 am

    No, the function parameter is a reference to an array of Len const chars. That’s how the function knows the length (assuming the last byte is a NUL terminator, hence the -1). The parentheses are there precisely to stop it being what you think it is.

    Actually there’s no such thing in C++ as an array of references, so it couldn’t be what you think it is even without the parens. I guess (but am not sure) that the need for the parens is just for consistency with other similar type definitions, such as pointers to arrays:

    void fn(const char *a[3]); // parameter a is of type const char**, the 3 is ignored.
    void fn(const char (*a)[3]; // parameter a is a pointer to an array of 3 const chars.
    

    That example also illustrates why an array is not a pointer. Predict the output of the following program, and then run it:

    #include <iostream>
    
    void fn(const char (*a)[3]) {
        std::cout << sizeof(a) << "\n" << sizeof(*a) << "\n";
    }
    
    void fn2(const char *a[3]) {
        std::cout << sizeof(a) << "\n" << sizeof(*a) << "\n";
    }
    
    int main() {
        const char a[3] = {};
        const char **b = 0;
        fn(&a);
        fn2(b);
    }
    
    #if 0 
    // error: declaration of `a' as array of references
    void fn3(const char & a[3]) {
        std::cout << sizeof(a) << "\n" << sizeof(*a) << "\n";
    }
    #endif
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a reference-type variable that is readonly , because the reference never change,
I have a function whose prototype is as follows: void foo(const char * data);
If I wish to simply rename a column (not change its type or constraints,
Is there anyway to change the content-type of an XML document, in the XML
Why does Resharper want you to change most variables to var type instead of
I passed a pointer ptr to a function whose prototype takes it as const
I have a linked list structure: struct SomeLinkedList { const char* bar; int lots_of_interesting_stuff_in_here;
I have a function that I want to use as an event handler: void
I'll start with the code: typedef std::vector<unsigned char> CharBuf; static const int RCV_BUF_SIZE =
Delphi 2009 has changed its string type to use 2 bytes to represent a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.