I tried constexpr and got an error.

Asked 2 years ago, Updated 2 years ago, 426 views

I copied something written by someone else.So I don't think there is any problem.When I looked into it in my own way, I was told that the current C++ version cannot be used in the version, but I would appreciate it if you could tell me how to use the constexpr or how the current version has the same role.

I am using VSCode on my MacBook Air.

int main(){
    // Program to find the average value

    // Only NUMBERS_SIZE is available for calculation
    static constexper int NUMBERS_SIZE=10;
    int numbers [NUMBERS_SIZE] = {1,2,3,5,5,2,10,100,50,33};

    // Print numbers[] to the screen for human confirmation
    for(inti=0;i<NUMBERS_SIZE;++i){
        printf("%d,", numbers[i]);
    }
    printf("\b\b\n";

    // calculate the average value from here
    // Declare variable average to store average values
    double average = 0.0;

    // First, find the sum of the numbers.
    for(inti=0;i<NUMBERS_SIZE;++i){
        average+=number[i];
    }

    // Then divide by the number of numbers.
    average / = NUMBERS_SIZE

    // I'm sure they wanted an answer.
    // Output average to the screen for human confirmation
    printf("average is %d\n", average);
    
    return 0;
}

c++ xcode macos

2022-09-30 22:04

2 Answers

There seem to be some mistakes.

  • #include<stdio.h> is not leading
  • constexpr is described as constexper and e is extra
  • average/=NUMBERS_SIZE line has no end-of-line ;
  • printf("average is %d\n", average); is not %d but %f

Below is the code that has been modified above.

#include<stdio.h>

int main() {
    // Program to find the average value

    // Only NUMBERS_SIZE is available for calculation
    static constexprint NUMBERS_SIZE=10;
    int numbers [NUMBERS_SIZE] = {1,2,3,5,5,2,10,100,50,33};

    // Print numbers[] to the screen for human confirmation
    for(inti=0;i<NUMBERS_SIZE;++i){
        printf("%d,", numbers[i]);
    }
    printf("\b\b\n";

    // calculate the average value from here
    // Declare variable average to store average values
    double average = 0.0;

    // First, find the sum of the numbers.
    for(inti=0;i<NUMBERS_SIZE;++i){
        average+=number[i];
    }

    // Then divide by the number of numbers.
    average /=NUMBERS_SIZE;

    // I'm sure they wanted an answer.
    // Output average to the screen for human confirmation
    printf("average is %f\n", average);
    
    return 0;
}

Assume that the compiler uses clang++ because the tag says xcode, but constexpr can only be used from C++11, so you can use constexpr by specifying -std=c++11 as the compilation option to clang++.

This time, I compiled directly from Terminal using clang++.
The command line is as follows:

clang++-std=c++11 constexpr.cpp

The results were as follows.

1,2,3,5,5,2,10,100,50,33
The average value is 21.100000.


2022-09-30 22:04

I took a picture of someone else's writing.

There is a problem with the source.

constexpr is a function of the C++ language, but as far as the code is concerned, it appears to be a C language code.There is no constexpr in C language, so it is natural that an error occurs.

What has the same role

The C++ language provides the std::size function.This allows the compiler to derive the array size.This means that misusing the number of elements in an array also deviates the average value, so you should leave the calculation of the array size to the compiler.

constexprint numbers[]={1,2,3,5,5,2,10,100,50,33};
constexpr size_t numbers_size=std::size(number);

The C++ language provides the std::accumulate function.

constexpr double total=std::accumulate(std::begin(number), std::end(number), 0.0);
constexpr double average=total/number_size;

As constexpr, the compiler actually does this calculation at compile time, where total contains 211 from the beginning, and average also contains 21.1.

In addition,

  • std::size(), std::begin(), and std::end() require #include<iterator> to use
  • std::accumulate() requires #include<numeric> to use

Each is required.

C++ language is evolving year by year, and it is also important to understand which features are available and when they are available. (Thank you for pointing it out, yohjp.)

You may also need to specify the language version you want to use for the compiler.


2022-09-30 22:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.