Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 368499

Summary: Error when using sizeof(T) as template parameter
Product: [Tools] CDT Reporter: Nathan Ridge <zeratul976>
Component: cdt-indexerAssignee: Sergey Prigogin <eclipse.sprigogin>
Status: RESOLVED FIXED QA Contact: Markus Schorn <mschorn.eclipse>
Severity: normal    
Priority: P3 CC: cdtdoug, eclipse.sprigogin, yevshif
Version: 8.1.0   
Target Milestone: 8.1.1   
Hardware: All   
OS: All   
Whiteboard:
Bug Depends on: 299911    
Bug Blocks:    

Description Nathan Ridge CLA 2012-01-12 20:54:26 EST
template <int> struct foo {};
template <> struct foo<1> { typedef int type; };

template <typename T>
struct A
{
    typedef typename foo<sizeof(T)>::type type;  // ERROR HERE
};


The error at the indicated line is: "Type 'foo<sizeof(T)>::type' could not be resolved"

There exist instantiations of A for which the line is valid (namely, those with sizeof(T) == 1), so it is incorrect to mark the line as an error.
Comment 1 Markus Schorn CLA 2012-01-13 01:18:08 EST
The example contains a dependent expression.
Comment 2 Nathan Ridge CLA 2012-01-13 02:24:50 EST
Hmm... it seems a lot of things use dependent expressions.

The example above shows up in the following metaprogramming trick:

template <typename T>
struct some_trait
{
    typedef char                  one;
    typedef struct { char x[2]; } two;

    one check(...);
    two check(some-arguments);

    static const bool value = sizeof(expr) == 1;
};

where 'expr' is some expression involving T, and 'expr' and the arguments to the second overload of check are devised to that the second overload is chosen for some types T, and the first for others.

This is used to implement type traits metafunctions like is_convertible<A, B>, which are used all over the place in Boost.

Is it difficult to add support for dependent expressions? Maybe just sizeof() at first, since it was already there in C++03, while decltype() is new to C++11?
Comment 3 Markus Schorn CLA 2012-01-13 07:53:25 EST
(In reply to comment #2)
> ...
> Is it difficult to add support for dependent expressions? Maybe just sizeof()
> at first, since it was already there in C++03, while decltype() is new to
> C++11?
It is not difficult in terms of complexity, however it is a rather large change. The change is large because a bunch of algorithms, including function overload resolution and calculation of expression-types, assume that they can pick up information directly from the AST (rather than being able to work on something that was specialized/instantiated).

Unfortunatly I am always short on time, however bug 299911 is one of the things I want to get done.
Comment 4 Nathan Ridge CLA 2012-03-12 04:40:26 EDT
(In reply to comment #3)
> (In reply to comment #2)
> > ...
> > Is it difficult to add support for dependent expressions? Maybe just sizeof()
> > at first, since it was already there in C++03, while decltype() is new to
> > C++11?
> It is not difficult in terms of complexity, however it is a rather large
> change. The change is large because a bunch of algorithms, including function
> overload resolution and calculation of expression-types, assume that they can
> pick up information directly from the AST (rather than being able to work on
> something that was specialized/instantiated).
> 
> Unfortunatly I am always short on time, however bug 299911 is one of the things
> I want to get done.

Any update on the support for dependent expressions? Does it have a chance of making the Juno release?
Comment 5 Markus Schorn CLA 2012-03-12 05:49:33 EDT
At the moment I have very little time to spend on CDT, I will not be able to look at this for Juno.
Comment 6 Sergey Prigogin CLA 2012-08-18 23:25:57 EDT
Fixed together with bug 299911.