According to the clause 29.5 of Standard std::atomic is defined as struct:
namespace std {
template <class T> struct atomic { ...
I would understand if it gave direct access to data members like std::pair, but it doesn't. Or if it used like functor, std::hash for example.
What is the main reason for std::atomic being struct, not class?
Related
Is there an equivalent in Swift to pass as an argument an empty interface{} like in Go?
I am not very familiar with Go, but I think the Swift equivalent would be an argument of type Any or AnyObject. You can't do much with such an argument other than try to cast it to a more specific type.
Go's interfaces and Swift's protocols are quite different:
Golang's interfaces are "structural" (akin to C++'s "Concepts"), meaning that their identity is defined by their structure. If a go struct has the structure required by an interface, it implicitly implements that interface. That is, the struct doesn't say anywhere "I am strust S, and I implement interface I."
Swift's protocols are "nominal", meaning that their identity is defined their name. If a Swift struct fulfills all of the requirements of a protocol, it doesn't conform to that protocol, unless an explicit struct S: P { ... } declaration is made (the body of that declaration can even be empty, but it's still necessary for the conformance to exist).
The closest analogue to interface{} in Swift is Any. It's built-in protocol to which all type conform. It gets special treatment, it's defined by the compiler, and has hard-coded logic to make all other types conform to it. You won't see protocol Any {} or struct S: Any {} declared anywhere explicitly. AnyObject is similar, and also gets special treatment. But no other Swift protocols do. Their conformance need to be explicit.
If I am not mistaking Swift's equivalent would be:
protocol EmptyProtocol {
}
In swift interfaces are called protocols don't ask me :D
Are you trying to do type erasure or something? Just curious...
Can nettype be used to define struct in SystemVerilog language?
I am getting failure in defining so, can anybody tell me?
A nettype cannot be used as a member of a struct. Only other data types can be used as struct members. A nettype is a kind of signal that can have a data type, including a struct, but a nettype itself is not a data type.
I am experimenting with protocols and how they can be used to decouple code, while taking advantage of other side effects of using protocols. Basically, what I am trying to do is the following:
An example could be a superset of API calls that is grouped into protocols by scope (eg "user API calls" and "settings API calls", or "non-mutating API calls" and "mutating API calls"). In this case, a consumer interested in using the API exposed by A should not necessarily know about the API's of B and C.
Consider the following protocols which mimic the above scenario:
protocol A {}
protocol B {}
protocol C {}
Then, given a set of protocols [A, B, C], using a Factory class, I would like an instance of an API object that conforms to a subset of these:
let a = Factory<A>.create()
let ab = Factory<A, B>.create()
let bc = Factory<B, C>.create()
I have tried a handfuld of implementations such as:
class Factory<T: protocol<A, B, C>> {
class func create() -> T {
return ...
}
}
However, initialising with let a = Factory<A>.create() yields the following error:
Using 'A' as a concrete type conforming to protocol 'A' is not supported
The same error occurs if Factory is defined as:
class Factory<T where T: A, T: B>
There are a few things that obviously fails:
<T: protocol<A, B, C>> and <T where T: A, T: B> requires the returned object to conform to both A, B, and C, not just a subset of them.
Given the error, it does not seem like it is even possible to "use a protocol as a concrete type conforming to a protocol".
In short, is it even possible to achieve what I am trying to?
I'd drop the use of generics in your case and I'd probably also look at protocol inheritance.
So, for example, a mutable protocol would inherit from the immutable protocol, because you want to be able to access as a minimum and both access and change in the alternate case.
The purpose of protocols is to abstract you from the underlying implementation classes. Generics are not really adding to that power in your example. Your factory (or factories) can simply provide a number of creation methods which return objects conforming to the various protocols.
To continue using generics means exposing the underlying implementation classes, and this is the cause for your error. You can't use a protocol for this because it isn't concrete, it's just the definition of what some concrete object is going to provide.
This question already has answers here:
Why Choose Struct Over Class?
(17 answers)
Closed 7 years ago.
I learn Swift from some time, I know the differences between structure and class. The main difference is structure is of value type and class is of reference type but didn't understand when to use structure instead of a class. Please explain it.
For example, In case of Protocols:
First, We have just a protocol of struct type:
protocol SomeProtocol{
func doSomeStuff()
}
Second, We make protocol of class type like this:
protocol SomeProtocol: class{
func doSomeStuff()
}
So, Please explain me, when we have to use protocol of struct type or of class type.
Firstly structs are passed by value (copied), and a class is passed by reference (copied just the memory address to the object).You may want to use structs for simpler types, because you get a free init for all the properties your struct has.And with protocols, the first one you can use it on class,struct and enum, the second you say that you only use that on classes,and you may want to put class if your protocol is a delegate or a data source,because you want the property(of the type of your protocol) weak to avoid the memory cycle. IMHO use classes for multi-scene apps because you don't need to take care to update value when you edited something in an another scene.
The protocol is not "of struct type" or "of class type", that is wrong terminology.
If you write SomeProtocol: class you make sure only classes can conform to that protocol, structs cannot. If you don't include the class both classes and structs can conform.
The docs (scroll down to "Class-Only Protocols") tell you that
You can limit protocol adoption to class types (and not structures or enumerations) by adding the class keyword to a protocol’s inheritance list. The class keyword must always appear first in a protocol’s inheritance list, before any inherited protocols.
Use a class-only protocol when the behavior defined by that protocol’s requirements assumes or requires that a conforming type has reference semantics rather than value semantics. For more on reference and value semantics, see Structures and Enumerations Are Value Types and Classes Are Reference Types.
So far this is what I understand objects are, I need feedback to know if I'm correct.
A class is made up of member functions. A class also defines types like int does.
An object is defined by that class and then the object calls the member functions within that class (only in the class it was defined by).
Need to know if I'm missing anything or if I'm wrong about something. Thanks
A class is made up of member functions.
Not necessarily, classes can contain data members too.
A class also defines types like int does.
True
Then the object calls the member functions within that class
Once, you go through the concepts of inheritance, you will understand that, an object can call methods of its base classes