News for hackers. Stuff that matters.
Re:Templating in C by bitwize on Saturday February 28, 2015 @06:57PM (#9125516)
Just use C++. The lack of semantically aware, type safe templates is one of the deficiencies of C that motivated the development of C++.

C++ is a strictly more powerful language than C. Play to its strengths rather than abusing C's weaker constructs.

Re:Templating in C by rdtsc on Saturday February 28, 2015 @07:33PM (#9125594)
> C++ is a strictly more powerful language than

Not true. C99 has variable length arrays. Those are nice for quick temporary buffers, and I have use them pretty often.

> Play to its strengths rather than abusing C's weaker constructs.

How do I play to its ABI compatiblity strengths? Because I have to keep wrapping all of it in extern "C" declaration in a large enough system.

Re:Templating in C by sjolsen on Sunday March 01, 2015 @02:24AM (#9126355)
>C99 has variable length arrays

But they don't give C any power C++ doesn't have. In terms of expressive power, you have things like std::vector, and in terms of performance, everyone except MSVC supports VLAs in C++ anyway, and MSVC doesn't even support them in C.

>How do I play to its ABI compatiblity strengths? Because I have to keep wrapping all of it in extern "C" declaration in a large enough system.

That is playing to its ABI compatibility strengths. In how many other languages is binary compatibility so simple?

Regardless, no one said anything about ABI compatibility being one of C++'s strengths. The point was about using C++'s template system for, of all things, exactly what it was designed to do.

Re:Templating in C by rdtsc on Sunday March 01, 2015 @11:54AM (#9127770)
> , you have things like std::vector, and in terms of performance, everyone except MSVC supports VLAs in C++ anyway, and MSVC doesn't even support them in C.

They are not the same. VLA in C are on the stack vectors are allocated from dynamic memory (heap).

> Regardless, no one said anything about ABI compatibility being one of C++'s strengths.

Right but that is why in practice C++ it is not a strict superset of C. When having to interface with other parts of the systems (loading .so, .dlls) or integrating with scripting langauges, the interface still has to be wrapped in a C interface.

Re:Templating in C by sjolsen on Monday March 02, 2015 @03:16AM (#9130870)
>They are not the same

I didn't say that they were. I said that they were equally expressive, because the claim was that C++ is more powerful than C.

>VLA in C are on the stack vectors are allocated from dynamic memory (heap)

That is not necessarily true. It's an implementation detail, and as far as it's relevant, so is the fact that everyone supports VLAs in C++.

>Right but that is why in practice C++ it is not a strict superset of C

Nobody said that C++ was a strict superset of C. The claim was that C++ is more powerful than C, and the fact that you have to specifically request ABI compatibility is not evidence to the contrary. Rather, the fact that you can do this at all (and that you can't use C++ linkage in C) is evidence in support of it.

Re:Templating in C by xamuel on Sunday March 01, 2015 @05:00AM (#9126581)
>C++ is a strictly more powerful language than C.

And PHP is a strictly more powerful language than HTML :)

Re:Templating in C by _RPM on Saturday February 28, 2015 @07:03PM (#9125525)
"Just use C++" isn't always an option.
Re:Templating in C by vinay427 on Saturday February 28, 2015 @07:32PM (#9125591)
Yep, as Stroustrup said: "I never saw a project for which C was better than C++ for any reason but the lack of a good C++ compiler."
Re:Templating in C by eru on Saturday February 28, 2015 @09:27PM (#9125804)
Linus Torvald disagrees. (His best argument for using C over C++ is to keep the people who prefer C++ over C at bay.)
Re:Templating in C by smegel on Saturday February 28, 2015 @09:05PM (#9125754)
C's strength is all the ways you can't blow your foot off.
Re:Templating in C by sjolsen on Sunday March 01, 2015 @02:31AM (#9126361)
And its weakness is that those ways in which you can, you're practically required to use to accomplish anything interesting. You can't even perform formatted output without throwing the entire type system out the window.
Re:Templating in C by xamuel on Sunday March 01, 2015 @04:59AM (#9126579)
You could, in principle, perform formatted output without throwing the entire type system out the window. How? Rather than pass the arguments themselves, pass them wrapped up in structures that indicate their type, e.g., struct type_wrapper {int type; void *ptr; int i; char c;}; Of course, you'd have to write your own custom version of printf, possibly deal with the mess which is va_copy, but it's doable.
Re:Templating in C by sjolsen on Monday March 02, 2015 @04:02AM (#9130980)
You're absolutely right of course; just about any facility's shortcomings can be overcome by redesigning that facility, but that's how we get Greenspun's Tenth Law. :)

Unfortunately, redesigning for safety/correctness in C almost always means a loss of place-of-use conciseness. For example, you'd have to manually wrap each argument to your hypothetical printf replacement in a "struct type_wrapper." Plus, there's no way to enforce that your format string has the same number of placeholders as you have arguments.

Speaking of which, you can actually do that in C++. It would look something like

    #include <cstddef>
    
    template <std::size_t N>
    static inline constexpr
    std::size_t num_args (const char (&fmt) [N]) {
    	// Count args
    }
    
    template <std::size_t N, typename... Args>
    void printf2_impl (const char* fmt, Args&&... args) {
    	static_assert (N == sizeof... (args), "Wrong number of arguments to printf2");
    	// Interleave args into fmt and print
    }
    
    #define printf2(fmt, ...) (printf2_impl <num_args (fmt)> (fmt, __VA_ARGS__))
You could even just typecheck the format string against your argument types and forward to printf. The biggest problem with this what happens when you give printf2 an empty __VA_ARGS__, but such is life when you're using the C preprocessor.