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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:33:47+00:00 2026-05-26T16:33:47+00:00

I am playing around with the new explicit for cast-operators. If you write something

  • 0

I am playing around with the new explicit for cast-operators. If you write something like

struct Data {
    explicit operator string(); 
};

It is not possible to accidentally convert Data to string. The darget data type bool is an exception: In certain cases the implicit conversion is allowed even if it is marked explicit — contextual conversion. So, you can use this data types in an if(...) for example:

struct Ok {
    explicit operator bool(); // allowed in if(...) anyway
};

The paragraph “25.4.(2) Sorting and related operations” seems to allow this for the Compare functor of standard containers like set as well. But my tries with gcc-4.7.0 fail, and I am note sure if it is my mis-understanding or a bug in gcc?

#include <set>

struct YesNo { // Return value type of Comperator
    int val_;
    explicit YesNo(int y) : val_{y} {}
    /* explicit */ operator bool() { return val_!=0; }
};

static const YesNo yes{1};
static const YesNo no{0};

struct LessYesNo {  // Comperator with special return values
    YesNo operator()(int a, int b) const {
        return a<b ? yes : no;
    }
};

int main() {
    std::set<int,LessYesNo> data {2,3,4,1,2};
}

Without the explicit before operator bool() the example compiles. And my understanding of “25.4.(2)” is, that this should also compile with the `explicit.

Did I understand the Std correctly that for set also explicit bool conversions should work? And might this be a bug in gcc then, or did I understand something wrong?

  • 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-26T16:33:48+00:00Added an answer on May 26, 2026 at 4:33 pm

    My reading of the standard is a little different –
    section 25.4 deals with sorting algorithms rather than for sorted containers; the context established in 25.4.(1) means that the property of the compare object specified in 25.4.(2) applies to the algorithms in 25.4, not to sorted containers

    1
    All the operations in 25.4 have two versions: one that takes a
    function object of type Compare and one that uses an operator.

    2
    Compare is a function object type (20.8). The return value of the
    function call operation applied to an object of type Compare, when
    contextually converted to bool (4), yields true if the first argument
    of the call is less than the second, and false otherwise. Compare comp
    is used throughout for algorithms assuming an ordering relation. It is
    assumed that comp will not apply any non-constant function through the
    dereferenced iterator.

    I don’t know whether your example should work or not, but I don’t think section 25.4 is applicable here.

    A quick test with a vector and std::sort works:

    #include <list>
    #include <algorithm>
    
    struct YesNo { // Return value type of Comperator
        int val_;
        explicit YesNo(int y) : val_{y} {}
        explicit operator bool() { return val_!=0; }
    };
    
    static const YesNo yes{1};
    static const YesNo no{0};
    
    struct LessYesNo {  // Comperator with special return values
        YesNo operator()(int a, int b) const {
            return a<b ? yes : no;
        }
    };
    
    int main() {
        std::vector<int> data {2,3,4,1,2};
        std::sort(std::begin(data), std::end(data), LessYesNo());
    }
    

    Edit:

    The associative container’s Compare parameter is defined in terms of secion 25.4:

    1 Associative containers provide fast retrieval of data based on keys. The library provides four basic kinds of associative containers: set, multiset, map and multimap.

    2 Each associative container is parameterized on Key and an ordering relation Compare that
    induces a strict weak ordering (25.4) on elements of Key. In addition, map and multimap associate an arbitrary type T with the Key. The object of type Compare is called the comparison object of a container.

    and 23. has no other conditions on the type of Compare as far as I can see, so it does seem reasonable to assume that a type satisfying the constraints of 25.4 are equally applicable.

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

Sidebar

Related Questions

While playing around with regexps in Scala I wrote something like this: scala> val
I have been playing around with Bundle.putStringArrayList ArrayList<String> myArrayList = new ArrayList<String>(); ArrayList<String> keys
i'm new to android development and i'm playing around with json data. I managed
I'm a very new to C#. Just playing around with it. Not for a
I am new jQuery, playing around with it. I would like text box when
Like a lot of developers I am playing around with the new features of
I'm playing around with a new C# email library and would like to test
I'm new to WPF. I have been been playing around with binding data to
I'm new to ruby and I'm playing around with the IRB. I found that
So I'm kind of new to VB and am just playing around with a

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.