CLANG: Elaborated type refers to a typedef error - clang

Here is my code snippet
template <class T>
struct ClassFriendMaker
{
typedef T Type;
};
template <class T>
class Singleton
{
friend class ClassFriendMaker<T>::Type; // Problem in this line
//Other declaration
}
When compiled with CLANG, it gave me an error:
error: elaborated type refers to a typedef
friend class ClassFriendMaker<T>::Type;
^
May I know what's wrong ? Thankyou

Replace class with typename in friend declaration
friend class ClassFriendMaker<T>::Type;
friend typename ClassFriendMaker<T>::Type;

Related

How to use covariant in constructors?

Minimum reproducible code:
class Foo {}
class Bar extends Foo {}
class Baz {
final void Function(Foo) f;
Baz._(this.f);
Baz.one(void Function(Bar) func) : this._(func); // Compile error
}
I am getting this error:
The argument type 'void Function(Bar)' can't be assigned to the parameter type 'void Function(Foo)'
How can I use the covariant keyword to tell analyzer that a call to f will return Bar in my particular case?
Note: I can use this._(func as void Function(Foo)) to make it compile, but I'm looking for a better solution.

Why does conceptual class template specialization cause an error

I tried to build the following with gcc 10 -std=gnu++20 -fconcepts:
template <std::signed_integral T>
class MyClass{ T a; };
template <std::unsigned_integral T>
class MyClass{ T a; };
Why does this code cause the following error?
> declaration of template parameter ‘class T’ with different constraints
> 55 | template <std::unsigned_integral T>
> | ^~~
Shouldn't it be fine?
Shouldn't it be fine?
No, constraints don't make classes "overloadable". You still need a primary template and then you need to specialize that template:
template <std::integral T>
class MyClass;
template <std::signed_integral T>
class MyClass<T>{ T a; };
template <std::unsigned_integral T>
class MyClass<T>{ T a; };

Java compiler error: raw type with method returning Optional

I'm trying to understand the Java compiler's thinking (I know, bad idea)...
Consider this program:
import java.util.Optional;
public class xx {
public static class Foo<T> {
public interface Bar<T> {
int getX();
}
public Optional<Bar<T>> getBar() {
return Optional.empty();
}
}
public static void main(String[] args) throws Exception {
Foo foo = new Foo(); // note raw type
foo.getBar().get().getX();
}
}
The java 1.8.0_112 compiler gives:
xx.java:15: error: cannot find symbol
foo.getBar().get().getX();
^
symbol: method getX()
location: class Object
1 error
The question is: why doesn't the compiler, given the raw type Foo for foo, realize that the return type of foo.getBar() is Optional<? extends Bar> instead of what it apparently thinks, which is Optional<?> ?
Note: I know how to change this program to make it compile, that's not the question.
Once you use raw types in conjunction with type inference, the following from JLS 18.5.2 will apply
If unchecked conversion was necessary for the method to be applicable during constraint set reduction in §18.5.1, then [...] the return type and thrown types of the invocation type of m are given by the erasure of the return type and thrown types of m's type.
From this follows, that the return type of foo.getBar() is indeed just Optional with all type arguments erased.
Solution: avoid raw types, always.

Analyzer warns that an argument type can't be itself

The error: The argument type 'AdvformBaseComponent(advform/base.dart)' can't be assigned to the parameter type 'AdvformBaseComponent(advform/base.dart)'.
The analyzer is warning that the argument type can't be AdvformBaseComponent, but the function is expecting that exact class as argument:
void addControl(AdvformBaseComponent baseComponent, String fieldName);
the below snippet is within a ngOnInit of AdvformBaseComponent.
objector.addControl(this, name);
It only hides the warning if I do a "addControl(this as dynamic", but that is so hacky and non performant...
sdk 1.22.1
EDIT:
The AdvformBaseComponent is an abstract class that is implemented by other components. It is essentially a base class for form components.
The AdvformObjectComponent is a panel group of AdvformBaseComponent based components. It is mandatory that any AdvformBaseComponent have a parent of AdvformObjectComponent.
They are in the same project and same directory.
I can share these files if you find it useful, they are browser components anyway.
abstract class AdvformBaseComponent implements OnInit, AfterViewInit,
OnDestroy {
AdvformObjectComponent objector;
AdvformBaseComponent(this.objector, #Optional() this._submitter, this.translator, #Optional() this._group) {
if (objector == null)
throw new Exception(
'Advform inputs must have a advform-object as parent.');
}
}
class AdvformObjectComponent implements OnInit {
void addControl(AdvformBaseComponent baseComponent, String fieldName) {
...
}
}
the error stopped after the 1.23.0

NS_ENUM error when declared before class instance variables

Example .h file:
#interface MyClass : NSObject
typedef NS_ENUM(int, myType) {
Something,
SomethingElse,
SomethingElseElse,
YetAnotherSomethingElse
};
{ //Error On This Line: Expected Identifier or '('
int aInstanceVariable;
}
//Some Methods go here
#end
Why am I getting that error (see the comment in the code above)? It works fine when below the class instance variable declaration, but I would like to use it as the type for one of my instance variables.
Thanks to #CarlVeazey, I discovered that the answer was simple: Move the typedef declaration to above #interface. The reason for this is that types cannot be owned by a class or an instance of a class, and therefore cannot be in the interface for a class.

Resources