3

I'm looking at some codes which makes heavy uses of templates. It compiles fine on GCC, but not on VS (tested on 2003 - 2010 beta 1), where it fails during syntax analysis. Unfortunately I don't know enough of the code structure to be able reduce the problem and reproduce it in only a few lines, so I can only guess at the cause. I'm hoping someone here can point me in the right direction.

We have

template< class UInt, typename IntT,
    bool is_signed = std::numeric_limits<IntT>::is_signed >
struct uii_ops_impl;

// ....

template<class UInt>
struct uii_ops_impl< UInt,
    typename make_signed<typename UInt::digit_type>::type, true >
{
    typedef UInt unbounded_int_type;
    typedef typename make_signed< typename unbounded_int_type::digit_type >::type
        integral_type;

    // ...

    static void add(unbounded_int_type& lhs, integral_type rhs);

    // ...
};

template<class UInt>
void uii_ops_impl<
    UInt, typename make_signed<typename UInt::digit_type>::type,
    true >::add(unbounded_int_type& lhs, integral_type rhs)
{
    // ....
}

When compiled on VS, the first error message (among many) it returns is

: error C2065: 'unbounded_int_type' : undeclared identifier

I mean, point at the typedef huh? :-S

EDIT:

It seems there's something to do with

typename make_signed<typename UInt::digit_type>::type

being used as a template parameter. Throughout the rest of the codes, similar typedefs being used in the member function parameter compiles fine. The only difference I can see so far is that none of the other cases have the above line as a template parameter. make_signed is from Boost.TypeTraits.

EDIT:

Okay, maybe that's not it, because the exact same thing is done in another file where it compiled fine. Hmm...

Bounty EDIT:

Okay, I think it's obvious at this point the problem is not actually where the compiler is complaining about. Only the two member functions definition at that particular point fail. It turns out that explicitly qualifying the parameter still doesn't compile. The only immediate solution is to define the function inline. That passes syntax analysis. However, when trying to instalize the template VS now fails because std::allocator<void> doesn't have a size_type member. Turns out VS have a specialization of std::allocator<T> for T=void that does not declare a size_type. I thought size_type is a required member of all allocators?

So the question now is, what could possibly foul up VS so much during syntax analysis that it complains about completely unrelated non-problem as errors, and how do you debug such codes?

p.s. For those that have too much time to spare, the code I'm trying to make work in VS is Kevin Sopp's mp_math in Boost's sandbox, which is based on libtommath.

8
  • Is this the first error it gives? If not, it's very likely an earlier error confused the compiler.
    – sbi
    Aug 13, 2009 at 10:49
  • * Point to sentence "the first error message (among many) ..." * :)
    – KTC
    Aug 13, 2009 at 11:10
  • Can you please put the test-case code that you used and that you found working with Visual C++? Maybe we can then figure out some differences to the original code. Aug 13, 2009 at 13:49
  • I've tried examples using similar template & replicating the entire structure of the codes up to the first error and they compiles, so now I'm going the other direction, with the full code, and stripping out all function definition and see if it compiles. I'll edit it when I have something.
    – KTC
    Aug 13, 2009 at 14:49
  • I'm looking at two different member function definition, with exactly the same signature, down to the letter, with one being in namespace say ns3 (that isn't compiling), and one being in namespace say ns4, inside ns3 where it is compiling... :(
    – KTC
    Aug 13, 2009 at 17:03

4 Answers 4

6
+50

I think this can be caused by a few circumstances

  • unbounded_int_type is a non-dependent type (defined at 14.6.2.1)
  • Its declaration appears in the class template. Because it's non-dependent, its name has to be resolved to a declaration at the time the member function is defined.

I suspect that Visual C++ is not able to do this lookup, and errors out instead. As someone else mentions, you can explicitly qualify the type-names in the member function definition. The types are then dependent, and this will trigger the compiler's mechanism to delay name lookup until instantiation.

template<class UInt>
void uii_ops_impl<
    UInt, typename make_signed<typename UInt::digit_type>::type,
    true >::add(typename
                /* repeat-that-beast uii_ops...true> here */
                   ::unbounded_int_type& lhs, 
                typename /* and here too */::integral_type rhs)
{
    // ....
}
3
  • It seems to me that the types for the argments for add() should follow the lookup rules in 3.4.1(8) - Unqualified name lookup. The rule that should apply (I think) is "shall be a member of class X or be a member of a base class of X", so the compiler should be able to resolve the name. As KTC indicated, the name lookup succeeds in the pared-down example; it's when some unspecified other stuff is in the mix that the problem crops up. Aug 13, 2009 at 0:57
  • I wasn't sure what example he used for the check. Something like struct X { typedef int Ty; void f(Ty); }; void X::f(Ty) { } could have worked, but of course is a much different situation :) Aug 13, 2009 at 1:05
  • In the other code you link to in your answer, the same situation as we have happens, but it still seems to work for them. Maybe that's because in this case, a partial specialization is involved. Aug 13, 2009 at 1:39
1

Here's something funny - this guy

ran into a bug with MSVC that's very similar to what you're seeing - except that using a typedef worked around the problem for him.

I still don't know what to make of the problems he ran into (or that you're running into). As you say, the small snippet you posted doesn't repro the error (given a simple make_signed<> template that lets make_signed<>::type compilable).

2
  • I actually saw that question earlier when I was searching for possible reasons I'm having problems with mine. His might have something to do with Argument Dependent Lookup. A qualified argument does not activate ADL, whereas an unqualified argument does.
    – KTC
    Aug 12, 2009 at 23:44
  • Except that the code in the other question won't be doing ADL (or it shoudln't) - the SetToFirst() function isn't being called, it's being defined. Anyway, I wasn't sure that it would help you in any way (or even if it was really related), but... it struck me as at least interesting. Aug 13, 2009 at 0:03
0

Which line causes the first error?

Does the use of unbounded_int_type in this definition:

template<class UInt>
void uii_ops_impl<
    UInt, typename make_signed<typename UInt::digit_type>::type,
    true >::add(unbounded_int_type& lhs, integral_type rhs)
{
    // ....
}

need to be fully qualified:

template<class UInt>
void uii_ops_impl<
    UInt, typename make_signed<typename UInt::digit_type>::type,
    true >::add(uii_ops_impl<UInt, etc ,true>::unbounded_int_type& lhs, integral_type rhs)
{
    // ....
}

?

1
  • The error occurs at the definition. It is possible to get rid of the error by explicitly qualifying the parameters, but toy example test shows that VS doesn't require that.
    – KTC
    Aug 12, 2009 at 23:40
0

You must help the compiler a bit on this one. You have to use the typename keyword because you have a qualified name that refers to a type and depends on a template parameter.

Think in this terms how can you be sure that unbounded_int_type::digit_type is a type? It depends on which type is unbounded_int_type. So you remove the ambiguity by adding the typename keyword.

template< class UInt, typename IntT,
bool is_signed = std::numeric_limits<IntT>::is_signed >
struct uii_ops_impl;

// ....

template <class T>
struct make_signed
{
    typedef T type;
};

template<class UInt>
struct uii_ops_impl< UInt,
    typename make_signed<typename UInt::digit_type>::type, true >
{
    typedef UInt unbounded_int_type;
    typedef typename make_signed< typename unbounded_int_type::digit_type >::type
        integral_type;

    // ...

    static void add(unbounded_int_type& lhs, integral_type rhs);

    // ...
};

template<class UInt>
void uii_ops_impl<
UInt, typename make_signed<typename UInt::digit_type>::type,
true >::add(unbounded_int_type& lhs, integral_type rhs)
{
    // ....
}

The only change is here - I added the typename keyword.

typedef
typename make_signed< typename unbounded_int_type::digit_type >::type             integral_type;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.