#Warning: initializer list?!

122 messages · Page 1 of 1 (latest)

split rivet
#

Why does the following line of code (line: 11) show warnings?

Student(std::string n) : name(n) {}
#include <iostream>
#include <string>
#include <vector>

class Student {
private:
    std::string name;
    std::vector<int> grades;

public:
    Student(std::string n) : name(n) {} 

    void addGrade(int grade) {
        if (grade >= 0 && grade <= 100) {
            grades.push_back(grade);
        }
    }

    double getAverage() {
        if (grades.empty()) return 0.0;
        double sum = 0.0;
        for (int grade : grades) {
            sum += grade;
        }
        return sum / grades.size();
    }

    void printInfo() {
        std::cout << name << "’s grades: ";
        for (int grade : grades) {
            std::cout << grade << " ";
        }
        std::cout << "(Avg: " << getAverage() << ")" << std::endl;
    }
};

int main() {
    Student student("Dana");
    student.addGrade(85);
    student.addGrade(90);
    student.printInfo();
    return 0;
}
limpid skiffBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

dawn geode
#

The actual output will explain more and better

fast oriole
#

these are not warnings, these are suggestions from the IDE

split rivet
dawn geode
#

Run the compiler and look at the warning/error output

split rivet
fast oriole
#

not always

#

if one particular suggestion is annoying you can turn it off in the settings

split rivet
dawn geode
#

clang-tidy has some good things, but a lot of bad

fast oriole
#

in other cases they may give you motivation to learn what it warns about

dawn geode
fast oriole
#

e.g. the first suggestion about explicit constructor

dawn geode
#

You can run the compiler directly or through your build system

fast oriole
#

you could do

Student bob = "Peepee poopoo";```
split rivet
fast oriole
#

which may or may not be desirable

#

you may not like lhs to be implicitly convertible to the target type like that

#

in which case declaring the constructor explicit would make

Student bob("Meow");```the only valid way to initialize a `Student`
fast oriole
trail smelt
fast oriole
#

^ that too

split rivet
#

What does that tell me?

fast oriole
#

that everything is fine

#

clang-tidy suggestions are what they are, suggestions

fast oriole
#

eh, not really

#

I just showed you that exists and what is the logic behind it

split rivet
fast oriole
split rivet
fast oriole
#

ah lol

trail smelt
fast oriole
#
Studeny bob = std::string("peepee poopoo");```
fast oriole
#

but by default clion has a ton of stuff enabled

#

which I personally find really annoying

#

especially the "declare constructor explicit"

split rivet
#

What is the difference between?

Student student = std::string("peepee poopoo");
Student student("peepee poopoo");
dawn geode
#

They're just different kinds of initialization

trail smelt
split rivet
fast oriole
#

yes, kinda

dawn geode
#

copy init vs direct init

fast oriole
#

but

#

it all comes down to: "do I want a string to be convertible to a student like that?"

dawn geode
fast oriole
#

awwww poor thing

split rivet
fast oriole
#

move semantics are a whole other beast

fast oriole
#

but a quick tl;dr is: using std::move may trigger a potentially more efficient copy

trail smelt
dawn geode
#

C++ initialization is really complicated. My advice is don't worry about all the details.

trail smelt
split rivet
split rivet
dawn geode
#

!init

limpid skiffBOT
dawn geode
#

There are like 9 kinds of initialization. But my advice is don't worry about this.

#

It doesn't matter nearly all of the time

trail smelt
#

oh there's a command

#

thats crazy

fast oriole
#

I just noticed

#

this thread is called "initializer list" but there is no initializer list anywhere to be found

#

scam! 😤

trail smelt
#

std::vector a = {10, 20, 30, 40, 50};

fast oriole
#

thank you kind sir/dudette

split rivet
#

What is narrowing conversion? Is it from float to double or double to float? 🤔

fast oriole
#

narrowing is a conversion from a one type to another that may not be able to represent the value

#

double to float is narrowing because a double typically has double the size of a float and a much greater precision

split rivet
#

@fast oriole I need explicit to avoid implicit conversion, right?

split rivet
#

Do all single-argument constructors need explicit? If so, why only for single-argument constructors?

fast oriole
#

no

#

nothing is "needed"

#

in some cases implicit conversions are desirable

#
std::string str = "hello";```is the best example
trail smelt
split rivet
fast oriole
# split rivet

clang-tidy is going to show this stupid warning all the time unless you turn it off

split rivet
fast oriole
#

I'm not saying you should turn off clang-tidy completely

#

go to your settings and configure what warnings you want to see

split rivet
fast oriole
#

its type is const char[6]

split rivet
fast oriole
#

but it's implicitly convertible to std::string

#

which is desirable by design

split rivet
fast oriole
#

not necessarily

split rivet
fast oriole
#

if by that you mean by default no string is of type std::string you'd be correct

#

std::string is a standard library type that co exists with raw char arrays

split rivet
#

Do we need 6 because of the backslash?

fast oriole
#

6th char is 0

split rivet
fast oriole
#

'\0' is a char with the value 0 yes

#

but anyway the point is that std::string has a constructor that takes const char*

#

and that constructor is NOT explicit

#

which allows implicit conversions from char arrays and string literals to std::string by design

#

which means, there is no "must" for using explicit in your constructors

#

it's all about design

split rivet
#

So std::string takes care of the characters that may confuse one

fast oriole
#

clang-tidy will show this warning regardless just cuz it's configured to do

fast oriole
split rivet
#

Can we say that C++ is easier than C (at least in this context)?

fast oriole
#

uh, probably

#

certainly safer

split rivet
fast oriole
split rivet
#

Yunan? 🇬🇷

fast oriole
#

yes