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 6848497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:52:32+00:00 2026-05-27T00:52:32+00:00

When it comes to constructors, adding the keyword explicit prevents an enthusiastic compiler from

  • 0

When it comes to constructors, adding the keyword explicit prevents an enthusiastic compiler from creating an object when it was not the programmer’s first intention. Is such mechanism available for casting operators too?

struct Foo
{
    operator std::string() const;
};

Here, for instance, I would like to be able to cast Foo into a std::string, but I don’t want such cast to happen implicitly.

  • 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. Editorial Team
    Editorial Team
    2026-05-27T00:52:32+00:00Added an answer on May 27, 2026 at 12:52 am

    Yes and No.

    It depends on which version of C++, you’re using.

    • C++98 and C++03 do not support explicit type conversion operators
    • But C++11 does.

    Example,

    struct A
    {
        //implicit conversion to int
        operator int() { return 100; }
    
        //explicit conversion to std::string
        explicit operator std::string() { return "explicit"; } 
    };
    
    int main() 
    {
       A a;
       int i = a;  //ok - implicit conversion 
       std::string s = a; //error - requires explicit conversion 
    }
    

    Compile it with g++ -std=c++0x, you will get this error:

    prog.cpp:13:20: error: conversion from ‘A’ to non-scalar type ‘std::string’ requested

    Online demo : http://ideone.com/DJut1

    But as soon as you write:

    std::string s = static_cast<std::string>(a); //ok - explicit conversion 
    

    The error goes away : http://ideone.com/LhuFd

    BTW, in C++11, the explicit conversion operator is referred to as "contextual conversion operator" if it converts to boolean. Also, if you want to know more about implicit and explicit conversions, read this topic:

    • Implicit VS Explicit Conversion
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It comes up from time to time and I'm wondering of it's possible to
I understand the use of the explicit keyword to avoid the implicit type conversions
When it comes to parsing numbers with decimal separators from string, Wicket's BigDecimalConverter behaves
When it comes to constructors, and assignments, and method calls, the PyCharm IDE is
i do not have any experience with virtual constructors which are available in Delphi.
Well I do not understand how it comes that Image class has a constructor
This a code is 10 years old, without error handling. The code comes from
In what order get the constructors of global object in C++ called? This question
Rails comes with a handy session hash into which we can cram stuff to
Django comes with CSRF protection middleware , which generates a unique per-session token for

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.