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

  • Home
  • SEARCH
  • 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 61735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:21:56+00:00 2026-05-10T18:21:56+00:00

I know you can use C++ keyword ‘explicit’ for constructors of classes to prevent

  • 0

I know you can use C++ keyword ‘explicit’ for constructors of classes to prevent an automatic conversion of type. Can you use this same command to prevent the conversion of parameters for a class method?

I have two class members, one which takes a bool as a param, the other an unsigned int. When I called the function with an int, the compiler converted the param to a bool and called the wrong method. I know eventually I’ll replace the bool, but for now don’t want to break the other routines as this new routine is developed.

  • 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. 2026-05-10T18:21:57+00:00Added an answer on May 10, 2026 at 6:21 pm

    No, you can’t use explicit, but you can use a templated function to catch the incorrect parameter types.

    With C++11, you can declare the templated function as deleted. Here is a simple example:

    #include <iostream>  struct Thing {     void Foo(int value) {         std::cout << 'Foo: value' << std::endl;     }      template <typename T>     void Foo(T value) = delete; }; 

    This gives the following error message if you try to call Thing::Foo with a size_t parameter:

    error: use of deleted function     ‘void Thing::Foo(T) [with T = long unsigned int]’ 

    In pre-C++11 code, it can be accomplished using an undefined private function instead.

    class ClassThatOnlyTakesBoolsAndUIntsAsArguments { public:   // Assume definitions for these exist elsewhere   void Method(bool arg1);   void Method(unsigned int arg1);    // Below just an example showing how to do the same thing with more arguments   void MethodWithMoreParms(bool arg1, SomeType& arg2);   void MethodWithMoreParms(unsigned int arg1, SomeType& arg2);  private:   // You can leave these undefined   template<typename T>   void Method(T arg1);    // Below just an example showing how to do the same thing with more arguments   template<typename T>   void MethodWithMoreParms(T arg1, SomeType& arg2); }; 

    The disadvantage is that the code and the error message are less clear in this case, so the C++11 option should be selected whenever available.

    Repeat this pattern for every method that takes the bool or unsigned int. Do not provide an implementation for the templatized version of the method.

    This will force the user to always explicitly call the bool or unsigned int version.

    Any attempt to call Method with a type other than bool or unsigned int will fail to compile because the member is private, subject to the standard exceptions to visibility rules, of course (friend, internal calls, etc.). If something that does have access calls the private method, you will get a linker error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 335k
  • Answers 335k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Create an interface for doing the Job, then implement 2000… May 14, 2026 at 3:33 am
  • Editorial Team
    Editorial Team added an answer EmptyHandle is an internal static property on RuntimeMethodHandle. From Reflector:… May 14, 2026 at 3:33 am
  • Editorial Team
    Editorial Team added an answer The function you're looking for is _vscprintf (or _vscwprintf). These… May 14, 2026 at 3:33 am

Related Questions

I know you can use C++ keyword 'explicit' for constructors of classes to prevent
I know there are few question about const correctness where it is stated that
In the current C++0x draft I've noticed they introduced some new explicit keywords to
Suppose there is a function with 1000 code of line named LongFunction, and we
Update: See the bottom of this question for a C# workaround. Hi there, Consider

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.