Affichage des articles dont le libellé est Lookup. Afficher tous les articles
Affichage des articles dont le libellé est Lookup. Afficher tous les articles

mardi 28 janvier 2014

How does the name lookup work in this case? topic




On 28.01.2014 21:47, Peter wrote:
> Consider this definition (namespace and class share the same name):
>
> namespace Foo
> {
> int x;
>
> class Foo
> {
> public:
> static int x;
> };
>
> int Foo::x;
> }
>
> I wondered what Foo::x would refer to with "using" directive used for namespace Foo: a global variable x in namespace Foo or static member of class Foo? Basically, I assumed the following code would not compile:
>
> int main()
> {
> using namespace Foo;
> Foo::x;
> return 0;
> }
>
> My reasoning went like this:
>
> - Foo::x is a global variable x from namespace Foo
> - Foo::Foo::x is a static member of class Foo from namespace Foo, but since
> "using" directive is applied, the namespace name can be omitted, thus Foo::x is also a static member of class Foo
> - conclusion: call to Foo::x in main() is ambiguous - it refers to two different entities


I think that is correct.

And I think probably C++11 §7.3.4/6, in the "Using directive" section,
applies:

"If name lookup finds a declaration for a name in two different
namespaces, and the declarations do not declare the same entity and do
not declare functions, the use of the name is ill-formed."

As it happens Visual C++ 12.0 appears to also think so:

[error]
original.cpp(19) : error C2872: 'Foo' : ambiguous symbol
could be 'Foo'
or 'original.cpp(8) : Foo::Foo'
[/error]


> However, the compiler I tested it with (one of g++ recent versions) had
> no trouble disambiguating this: experiments showed Foo::x in main() is
> interpreted as global variable x in namespace Foo. Moreover, if I remove
> the definition of global x from namespace Foo, then the compiler emits
> the following error:
>
> main.cpp: In function 'int main()':
> main.cpp:16:4: error: 'x' is not a member of 'Foo'
> Foo::x;
>
> so it doesn't find the static member of class Foo.


Except that I believe that's wrong, it's reasonable: maybe its how the
language should be.


> In order for the compiler to find it I have to qualify it fully as
> Foo::Foo::x despite the "using namespace Foo;" line.


That's not full qualification, and Visual C++ still emits the diagnostic
above.

Full qualification is

::Foo::Foo::x;

and this is accepted by Visual C++.


> Why?


I think the ambiguity is real and that the g++ failure to diagnose it is
a compiler bug. I could be wrong. But, since the two compilers disagree,
at least one of Visual C++ and g++ has a bug here. ;-)


> How does the lookup work here?


C++11 §7.3.4/2

"A using-directive specifies that the names in the nominated namespace
can be used in the scope in which the using-directive appears after the
using-directive. During unqualified name lookup (3.4.1), the names
appear as if they were declared in the nearest enclosing namespace which
contains both the using-directive and the nominated namespace"


Cheers & hth.,

- Alf






samedi 25 janvier 2014

Can't get sqlite3.Row working: keyword lookup doesn't work topic




Hello,

using Python 2.7.6

I try to access a sqlite database using keyword lookup instead of
position (much more easy to maintain code), but it always fail, with the
error:
Index must be int or string

I have created the database, populated it, and here is the code that
tries to retrieve the information:

with sqlite3.connect(mydbPath) as db: # open the database
db.row_factory = sqlite3.Row
cursor = db.cursor()
cursor.execute("SELECT * FROM files")

for row in cursor.fetchall():
print(row.keys())
print(row["filename"])


result is:

['filename', 'filepath', 'filetag', 'PROJECT', 'SCENE', 'TAKE', 'TAPE',
'CIRCLED', 'FILE_UID', 'UBITS', 'TOTAL_FILES', 'FAMILY_UID', 'track_1',
'track_2', 'track_3', 'track_4', 'track_5', 'track_6', 'track_7',
'track_8', 'track_9', 'track_10', 'track_11', 'track_12', 'NOTE',
'duration', 'BWF_ORIGINATION_DATE', 'TIMECODE_FLAG', 'TIMECODE_RATE',
'FILE_SAMPLE_RATE', 'AUDIO_BIT_DEPTH', 'DIGITIZER_SAMPLE_RATE',
'TIMESTAMP_SAMPLE_RATE', 'TIMESTAMP_SINCE_MIDNIGHT', 'is_Short',
'is_MS', 'is_renamed_MS', 'WF_created', 'max_level', 'is_silent',
'is_silent_moved', 'silent_path', 'is_WS', 'is_WS_copied', 'CSV_made',
'is_cantar', 'is_sound_devices', 'exist']

error => Index must be int or string

What is wrong?

thanks a lot.