Click here to Skip to main content
15,895,142 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
PraiseRe: Whose idea was this in C\? Pin
Randor 17-Mar-22 5:05
professional Randor 17-Mar-22 5:05 
GeneralRe: Whose idea was this in C\? Pin
Kate-X25717-Mar-22 4:57
Kate-X25717-Mar-22 4:57 
GeneralRe: Whose idea was this in C\? Pin
Greg Utas17-Mar-22 6:00
professionalGreg Utas17-Mar-22 6:00 
GeneralRe: Whose idea was this in C\? Pin
Ralph Trickey17-Mar-22 11:31
Ralph Trickey17-Mar-22 11:31 
GeneralRe: Whose idea was this in C\? Pin
Greg Utas17-Mar-22 11:48
professionalGreg Utas17-Mar-22 11:48 
GeneralRe: Whose idea was this in C\? Pin
Dave B 202217-Mar-22 3:31
Dave B 202217-Mar-22 3:31 
GeneralRe: Whose idea was this in C\? Pin
Greg Utas17-Mar-22 3:54
professionalGreg Utas17-Mar-22 3:54 
GeneralRe: Whose idea was this in C\? Pin
Dave B 202217-Mar-22 5:31
Dave B 202217-Mar-22 5:31 
Greg, thank you for the reply.

It looks like the reason auto makes this more efficient that I was recalling was wrong. Although my conclusion is somewhat the same. Rereading the AAA article, I picked out the point:
Quote:
It is efficient by default and guarantees that no implicit conversions (including narrowing conversions), temporary objects, or wrapper indirections will occur. In particular, prefer using auto instead of function<> to name lambdas unless you need the type erasure and indirection.


The same is true of C# if one uses implicit conversions as can be seen in the following code:

public class T1 {
    int m_count;

    public T1(int cnt) 
    {
        m_count = cnt;
    }

    public static implicit operator T2(T1 t1) {
        return new T2(t1.m_count.ToString());
    }
}

public class T2 {
    string m_count;
    public T2(string count) {
        m_count = count;
    }
}

public class Class1
{
    T1 GenerateValue() {
        return new T1(22);
    }

    void temp() {
        
        // The following creates an instance of T1, converts it to T2 and 
        // leaves T1 for the GC to clean up.
        // Is it clear to a developer that they just created this overhead?
        T2 t2_implicit = GenerateValue();

        // The following creates and holds an instance of T1
        T1 t1_explicit = GenerateValue();

        // The following also creates and holds an instance of T1
        var t1_implicit = GenerateValue();
    }
}


This means my original statement was in error. The reason we "benefit" from not using var has little to do with us not using structs. It is because we don't use implicit conversions on the vast majority of our classes/structs. So the potential "mistake" of accidentally forcing an unnecessary type conversion is minimal and is outweighed by putting type information at the developers fingertips.

Quote:
Although I haven't used C#, I'd be surprised if best practices for when to use the heap versus the stack weren't the same in both languages.

The thing that makes C# different from C++ in this case is that all memory created to hold instances of a class cannot be placed on the stack. Class instance memory is always placed on the heap and managed by the garbage collector (GC). On the other hand, a struct is always considered a value type and storage follows the same as it would for an int or other primitive. That means it is either on the stack or occupying space in a (typically larger) object on the heap. You also can't hold a reference to a struct in another object. I think this tends to modify design patterns a bit between the languages.

Since you clearly know modern C++ better than I do, perhaps you could comment on how common it is to use implicit type conversion (constructors or operators) in classes such that common programming practices need to guard against unintentional implicit conversion.
GeneralRe: Whose idea was this in C\? Pin
Greg Utas17-Mar-22 5:55
professionalGreg Utas17-Mar-22 5:55 
GeneralRe: Whose idea was this in C\? Pin
Kate-X25717-Mar-22 4:57
Kate-X25717-Mar-22 4:57 
GeneralRe: Whose idea was this in C\? Pin
Slacker00716-Mar-22 5:39
professionalSlacker00716-Mar-22 5:39 
GeneralRe: Whose idea was this in C\? Pin
JustDre17-Mar-22 7:05
JustDre17-Mar-22 7:05 
GeneralRe: Whose idea was this in C\? Pin
TNCaver16-Mar-22 6:16
TNCaver16-Mar-22 6:16 
GeneralRe: Whose idea was this in C\? Pin
Ralph Trickey17-Mar-22 12:22
Ralph Trickey17-Mar-22 12:22 
GeneralRe: Whose idea was this in C\? Pin
Kate-X25717-Mar-22 4:57
Kate-X25717-Mar-22 4:57 
GeneralRe: Whose idea was this in C\? Pin
Kirk 1038982117-Mar-22 4:27
Kirk 1038982117-Mar-22 4:27 
GeneralRe: Whose idea was this in C\? Pin
Dave B 202217-Mar-22 6:34
Dave B 202217-Mar-22 6:34 
NewsInteresting article on the future of Wordle Clones, Archives, and Word List Pin
Slacker00716-Mar-22 0:26
professionalSlacker00716-Mar-22 0:26 
GeneralRe: Interesting article on the future of Wordle Clones, Archives, and Word List Pin
dan!sh 16-Mar-22 0:54
professional dan!sh 16-Mar-22 0:54 
GeneralRe: Interesting article on the future of Wordle Clones, Archives, and Word List Pin
Ravi Bhavnani16-Mar-22 4:51
professionalRavi Bhavnani16-Mar-22 4:51 
GeneralRe: Interesting article on the future of Wordle Clones, Archives, and Word List Pin
Kirk 1038982117-Mar-22 9:06
Kirk 1038982117-Mar-22 9:06 
GeneralRe: Interesting article on the future of Wordle Clones, Archives, and Word List Pin
den2k8816-Mar-22 2:18
professionalden2k8816-Mar-22 2:18 
GeneralRe: Interesting article on the future of Wordle Clones, Archives, and Word List Pin
Ravi Bhavnani16-Mar-22 4:50
professionalRavi Bhavnani16-Mar-22 4:50 
GeneralRe: Interesting article on the future of Wordle Clones, Archives, and Word List Pin
Rage16-Mar-22 5:10
professionalRage16-Mar-22 5:10 
GeneralRe: Interesting article on the future of Wordle Clones, Archives, and Word List Pin
den2k8816-Mar-22 22:44
professionalden2k8816-Mar-22 22:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.