[log] Unable to use own severity struct encapsulated in namespace

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[log] Unable to use own severity struct encapsulated in namespace

Boost - Users mailing list
Hi folks,

When taking this example [1] and modifying the enum 'severity_level'
like this:

namespace x {
        struct test
        {
                enum severity_level
                {
                        normal,
                        notification,
                        warning,
                        error,
                        critical
                };
        };
}

and replacing all references to severity_level with the appropriate
x::test::severity_level the 'operator<<' overload for the severity_level
is not issued. Can anyone help me with this one?

I can supply the complete modified example if needed


Cheers
Sebastian

[1]
https://www.boost.org/doc/libs/1_58_0/libs/log/example/doc/tutorial_attributes.cpp
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [log] Unable to use own severity struct encapsulated in namespace

Boost - Users mailing list
I've solved the issue:

In case anyone is interested: You need to put the ostream& operator<<
into the namespace of the severity-enum to make it work.

Cheers
Sebastian > Hi folks,

>
> When taking this example [1] and modifying the enum 'severity_level'
> like this:
>
> namespace x {
>      struct test
>      {
>          enum severity_level
>          {
>              normal,
>              notification,
>              warning,
>              error,
>              critical
>          };
>      };
> }
>
> and replacing all references to severity_level with the appropriate
> x::test::severity_level the 'operator<<' overload for the severity_level
> is not issued. Can anyone help me with this one?
>
> I can supply the complete modified example if needed
>
>
> Cheers
> Sebastian
>
> [1]
> https://www.boost.org/doc/libs/1_58_0/libs/log/example/doc/tutorial_attributes.cpp 
>
> _______________________________________________
> Boost-users mailing list
> [hidden email]
> https://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [log] Unable to use own severity struct encapsulated in namespace

Boost - Users mailing list


On 4 April 2018 at 11:24, Sebastian Messerschmidt via Boost-users <[hidden email]> wrote:
I've solved the issue:

In case anyone is interested: You need to put the ostream& operator<< into the namespace of the severity-enum to make it work.

This is called ADL - Argument Dependent Lookup.

When c++ encounters a namespace-unqualified function call, it will consider the current namespace, the global namespace, and the namespaces enclosing the type of each of the function's arguments.

For this reason, it's good practice to declare all free functions (including operators) that operate on user defined types in the namespace of those types.

 

Cheers
Sebastian > Hi folks,


When taking this example [1] and modifying the enum 'severity_level' like this:

namespace x {
     struct test
     {
         enum severity_level
         {
             normal,
             notification,
             warning,
             error,
             critical
         };
     };
}

and replacing all references to severity_level with the appropriate x::test::severity_level the 'operator<<' overload for the severity_level is not issued. Can anyone help me with this one?

I can supply the complete modified example if needed


Cheers
Sebastian

[1] https://www.boost.org/doc/libs/1_58_0/libs/log/example/doc/tutorial_attributes.cpp
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users


_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [log] Unable to use own severity struct encapsulated in namespace

Boost - Users mailing list
Hi Richard,

Thank you for the insight and clarifications. The problem with this is
certainly, that as an user you're left unaware, that the compiler didn't
pick your specialized overload, since enums are still sort of glorified
integers when it comes to types.

Cheers
Sebastain >

>
> On 4 April 2018 at 11:24, Sebastian Messerschmidt via Boost-users
> <[hidden email] <mailto:[hidden email]>> wrote:
>
>     I've solved the issue:
>
>     In case anyone is interested: You need to put the ostream&
>     operator<< into the namespace of the severity-enum to make it work.
>
>
> This is called ADL - Argument Dependent Lookup.
>
> When c++ encounters a namespace-unqualified function call, it will
> consider the current namespace, the global namespace, and the namespaces
> enclosing the _/type/_ of each of the function's arguments.
>
> For this reason, it's good practice to declare all free functions
> (including operators) that operate on user defined types in the
> namespace of those types.
>
>
>
>     Cheers
>     Sebastian > Hi folks,
>
>
>         When taking this example [1] and modifying the enum
>         'severity_level' like this:
>
>         namespace x {
>               struct test
>               {
>                   enum severity_level
>                   {
>                       normal,
>                       notification,
>                       warning,
>                       error,
>                       critical
>                   };
>               };
>         }
>
>         and replacing all references to severity_level with the
>         appropriate x::test::severity_level the 'operator<<' overload
>         for the severity_level is not issued. Can anyone help me with
>         this one?
>
>         I can supply the complete modified example if needed
>
>
>         Cheers
>         Sebastian
>
>         [1]
>         https://www.boost.org/doc/libs/1_58_0/libs/log/example/doc/tutorial_attributes.cpp
>         <https://www.boost.org/doc/libs/1_58_0/libs/log/example/doc/tutorial_attributes.cpp>
>
>         _______________________________________________
>         Boost-users mailing list
>         [hidden email] <mailto:[hidden email]>
>         https://lists.boost.org/mailman/listinfo.cgi/boost-users
>         <https://lists.boost.org/mailman/listinfo.cgi/boost-users>
>
>     _______________________________________________
>     Boost-users mailing list
>     [hidden email] <mailto:[hidden email]>
>     https://lists.boost.org/mailman/listinfo.cgi/boost-users
>     <https://lists.boost.org/mailman/listinfo.cgi/boost-users>
>
>
>
>
> _______________________________________________
> Boost-users mailing list
> [hidden email]
> https://lists.boost.org/mailman/listinfo.cgi/boost-users
>
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [log] Unable to use own severity struct encapsulated in namespace

Boost - Users mailing list


On Fri, 2018-04-06 at 09:39 +0200, Sebastian Messerschmidt wrote:

> Hi Richard,
>
> Thank you for the insight and clarifications. The problem with this
> is
> certainly, that as an user you're left unaware, that the compiler
> didn't
> pick your specialized overload, since enums are still sort of
> glorified
> integers when it comes to types.
>
Yes, it's one of those "awesome but problematic for beginners" areas of
c++.

ADL allows us to write code in terms of semantics rather than
specifics, which is the awesome part.

But the problem as you say, is that it takes experience to realise
where which overload will be selected - particularly with free
functions for which there is an overload for more than one class in an
object's public class hierarchy.

Some discipline (and documentation) is often required.

The good news is that if there is ambiguity, the compiler will demand
programmer intervention, so at least the overloads selection is
predictable, provided you know about the existence of all overloads.

R


> Cheers
> Sebastain >
> >
> > On 4 April 2018 at 11:24, Sebastian Messerschmidt via Boost-users
> > <[hidden email] <mailto:[hidden email]>>
> > wrote:
> >
> >     I've solved the issue:
> >
> >     In case anyone is interested: You need to put the ostream&
> >     operator<< into the namespace of the severity-enum to make it
> > work.
> >
> >
> > This is called ADL - Argument Dependent Lookup.
> >
> > When c++ encounters a namespace-unqualified function call, it will
> > consider the current namespace, the global namespace, and the
> > namespaces
> > enclosing the _/type/_ of each of the function's arguments.
> >
> > For this reason, it's good practice to declare all free functions
> > (including operators) that operate on user defined types in the
> > namespace of those types.
> >
> >
> >
> >     Cheers
> >     Sebastian > Hi folks,
> >
> >
> >         When taking this example [1] and modifying the enum
> >         'severity_level' like this:
> >
> >         namespace x {
> >               struct test
> >               {
> >                   enum severity_level
> >                   {
> >                       normal,
> >                       notification,
> >                       warning,
> >                       error,
> >                       critical
> >                   };
> >               };
> >         }
> >
> >         and replacing all references to severity_level with the
> >         appropriate x::test::severity_level the 'operator<<'
> > overload
> >         for the severity_level is not issued. Can anyone help me
> > with
> >         this one?
> >
> >         I can supply the complete modified example if needed
> >
> >
> >         Cheers
> >         Sebastian
> >
> >         [1]
> >         https://www.boost.org/doc/libs/1_58_0/libs/log/example/doc/
> > tutorial_attributes.cpp
> >         <https://www.boost.org/doc/libs/1_58_0/libs/log/example/doc
> > /tutorial_attributes.cpp>
> >
> >         _______________________________________________
> >         Boost-users mailing list
> >         [hidden email] <mailto:[hidden email]
> > .org>
> >         https://lists.boost.org/mailman/listinfo.cgi/boost-users
> >         <https://lists.boost.org/mailman/listinfo.cgi/boost-users>
> >
> >     _______________________________________________
> >     Boost-users mailing list
> >     [hidden email] <mailto:[hidden email]
> > >
> >     https://lists.boost.org/mailman/listinfo.cgi/boost-users
> >     <https://lists.boost.org/mailman/listinfo.cgi/boost-users>
> >
> >
> >
> >
> > _______________________________________________
> > Boost-users mailing list
> > [hidden email]
> > https://lists.boost.org/mailman/listinfo.cgi/boost-users
> >
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users

signature.asc (849 bytes) Download Attachment