Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add explicit to single-parameter constructors #28

Closed
NewbieOrange opened this issue Dec 16, 2021 · 5 comments
Closed

Add explicit to single-parameter constructors #28

NewbieOrange opened this issue Dec 16, 2021 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@NewbieOrange
Copy link

Describe the new feature

Can we add explicit to single-parameter constructors to avoid unintended implicit construct?

Code example

    explicit thread_pool(const ui32 &_thread_count = std::thread::hardware_concurrency())
        : thread_count(_thread_count ? _thread_count : std::thread::hardware_concurrency()), threads(new std::thread[thread_count]);
// ...
    explicit synced_stream(std::ostream &_out_stream = std::cout);
@NewbieOrange NewbieOrange added the enhancement New feature or request label Dec 16, 2021
@bshoshany
Copy link
Owner

Thanks for the suggestion! I'm not opposed to adding the explicit keyword, but given that there is only one constructor for each of these classes, under what circumstances will implicit constructors be an issue?

@NewbieOrange
Copy link
Author

To disallow passing an uint32 to a function taking, for example, thread_pool as a const ref, therefore forcing users to explicitly call the constructor to avoid confusion or unintended object construction.

@bshoshany
Copy link
Owner

I'm still not convinced. Would you mind writing a short code example illustrating a case where this might be relevant? Thanks!

@NewbieOrange
Copy link
Author

NewbieOrange commented Dec 22, 2021

It may be a silly error, but:

std::uint32_t thread_cnt = 10;
thread_pool thread_pool{thread_cnt};

void f(const thread_pool& pool) {
  // do something
}

f(thread_cnt); // error-prone, passed wrong parameter but still compiles

@bshoshany
Copy link
Owner

Okay, I see what you mean. I'll add this to my TODO list, but it will only be added when the new version is released (which will have many other changes and new features). It might take a few weeks or even months since I'm really busy these days with teaching and research. Thanks for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants