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 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

Related Questions

PHP classes can use the keyword self in a static context, like this: <?php
I know that I can use virtual keyword to tell the entity framework that
I know that in T-SQL (Server 2008 R2) I can use the 'Output' keyword
I would like to know, can I use insert keyword to add elements into
With an Array I know you can use the delete keyword to remove items
In C# I know you can use the default keyword to assign default values
I know I can use the -n flag in git grep to show the
I know you can use -fno-objc-arc flag to disable ARC for files that NOT
I know I can use install-data-hook to do anything I want after my data
I know you can use source control software for source code, but can you

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.