Can you use keyword explicit to prevent automatic conversion of method parameters?

后端 未结 8 1410
心在旅途
心在旅途 2020-12-04 17:18

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

相关标签:
8条回答
  • 2020-12-04 17:58

    You could also write an int version that calls the bool one.

    0 讨论(0)
  • 2020-12-04 18:05

    The following is a very basic wrapper that can be used to create a strong typedef:

    template <typename V, class D> 
    class StrongType
    {
    public:
      inline explicit StrongType(V const &v)
      : m_v(v)
      {}
    
      inline operator V () const
      {
        return m_v;
      }
    
    private:
      V m_v; // use V as "inner" type
    };
    
    class Tag1;
    typedef StrongType<int, Tag1> Tag1Type;
    
    
    void b1 (Tag1Type);
    
    void b2 (int i)
    {
      b1 (Tag1Type (i));
      b1 (i);                // Error
    }
    

    One nice feature of this approach, is that you can also distinguish between different parameters with the same type. For example you could have the following:

    class WidthTag;
    typedef StrongType<int, WidthTag> Width;  
    class HeightTag;
    typedef StrongType<int, HeightTag> Height;  
    
    void foo (Width width, Height height);
    

    It will be clear to the clients of 'foo' which argument is which.

    0 讨论(0)
  • 2020-12-04 18:06

    Something that might work for you is to use templates. The following shows the template function foo<>() being specialized for bool, unsigned int, and int. The main() function shows how the calls get resolved. Note that the calls that use a constant int that don't specify a type suffix will resolve to foo<int>(), so you'll get an error calling foo( 1) if you don't specialize on int. If this is the case, callers using a literal integer constant will have to use the "U" suffix to get the call to resolve (this might be the behavior you want).

    Otherwise you'll have to specialize on int and use the "U" suffix or cast it to an unsigned int before passing it on to the unsigned int version (or maybe do an assert that the value isn't negative, if that's what you want).

    #include <stdio.h>
    
    template <typename T>
    void foo( T);
    
    template <>
    void foo<bool>( bool x)
    {
        printf( "foo( bool)\n");
    }
    
    
    template <>
    void foo<unsigned int>( unsigned int x)
    {
        printf( "foo( unsigned int)\n");
    }
    
    
    template <>
    void foo<int>( int x)
    {
        printf( "foo( int)\n");
    }
    
    
    
    int main () 
    {
        foo( true);
        foo( false);
        foo( static_cast<unsigned int>( 0));
        foo( 0U);
        foo( 1U);
        foo( 2U);
        foo( 0);
        foo( 1);
        foo( 2);
    }
    
    0 讨论(0)
  • 2020-12-04 18:13

    The currently accepted answer (using a private templated function) is nice, but outdated. With C++11, we can use deleted functions instead:

    #include <iostream>
    
    struct Thing {
        void Foo(int value) {
            std::cout << "Foo: value" << std::endl;
        }
    
        template <typename T>
        void Foo(T value) = delete;
    };
    
    int main() {
        Thing t;
        int int_value = 1;
        size_t size_t_value = 2;
    
        t.Foo(int_value);
    
        // t.Foo(size_t_value);  // fails with below error
        // error: use of deleted function
        //   ‘void Thing::Foo(T) [with T = long unsigned int]’
    
        return 0;
    }
    

    This conveys the intent of the source code more directly and supplies the user with a clearer error message when trying to use the function with disallowed parameter types.

    0 讨论(0)
  • bool IS an int that is limited to either 0 or 1. That is the whole concept of return 0;, it is logically the same as saying return false;(don't use this in code though).

    0 讨论(0)
  • 2020-12-04 18:17

    Compiler gave "ambiguous call" warning, which will be sufficient.

    I was doing TDD development and didn't realize I forgot to implement the corresponding call in the mock object.

    0 讨论(0)
提交回复
热议问题