« first day (2328 days earlier)      last day (529 days later) » 

7:09 AM
It is a pity that, this code snippet does not compile.
Here is the code snippet:
#include <iostream>

class Foo
{
public:
int x;
int y;
};

std::ostream& operator<<(std::ostream& os, const Foo& foo)
{
std::cout << "[" << foo.x << "," <<"]" << std::endl;

return os;
}

template<typename T>
void do_sth(T data)
{
if (std::is_same<T, unsigned char>::value ||
std::is_same<T, char>::value)
{
std::cout << "1:" << static_cast<int>(data);
}
else
{
std::cout << "2:" << data;
}
}

int main()
{
do_sth(1);
do_sth(Foo());
}
Here is what the compiler complains: invalid 'static_cast' from type 'Foo' to type 'int'
23 | std::cout << "1:" << static_cast<int>(data);
I know what the compiler complains, but I don't fully understand it yet.
When T is not the type of char, std::cout << "1:" << static_cast<int>(data); should not be run. So why the compiler complains?
 
7:54 AM
@John Seems like your compiler is deferring the evaluation of std::is_same until run-time. Adding constexpr before if statement should do the trick.
if constexpr (std::is_same<T, char>::value ||
        std::is_same<T, char>::value)
 
8:15 AM
@domdrag It works indeed! Amazing!
But it's a pity that it's only supported by C++17 and later. I have to use C++11.
 
8:35 AM
But it would make your code uglier that's for sure.
@John But to answer your question; static_cast check is evaluated at compile time.
 
nwp
8:54 AM
"Tag dispatch" is related.
 
nwp
9:27 AM
Regular overloading should be good enough already. This is tag dispatch.
(It took me way too long to translate the "or")
Also note that 1 is an int and not a char or unsigned char. You probably meant '1' in the example.
 
9:40 AM
I see. Thank you very much.
 
 
2 hours later…
11:52 AM
It's a pity that some APIs provided by others which are out of my control and the said APIs return the same type(`uint8_t` other than `enum` type).
But the meaning is different when the same value is returned by different API. I hope to use overloading to convert the returned value to readable string.
Howerver, as per this [answer](https://stackoverflow.com/a/10595263/13611002), which says:
a typedef is just another name for the same type. But you can only overload on different types.
I thought and thought, I found a solution. But I think there must be better one.
#include <iostream>
#include <map>


namespace Demo
{
	class Foo
	{
		public:
		 using SampleType = uint8_t;
		 SampleType GetFooData(){return SampleType{1};}
	};

	class Bar
	{
		public:
		 using SampleType = uint8_t;
		 SampleType GetBarData(){return SampleType{2};}
	};
}

//////////////////code above is provided by others/////////////
template <typename T>
void FindAndPrint(const T& val, const std::map<T, std::string>& mp)
{
	auto itr = mp.find(static_cast<T>(val));
	if(itr != mp.end())
	{
		std::cout << itr->second;
 
nwp
I dislike that it's a user-side solution and not encapsulated away. Print<Demo::Bar>(foo.GetFooData()); is an easy mistake to make. Writing a class MyFoo and MyBar that encapsulate Demo::Foo and Demo::Bar and cast the uint8_ts to a proper printable enum seems easier to use and more difficult to mess up.
 
nwp
12:11 PM
As to the question in the comment, Demo::Foo::SampleType and Demo::Bar::SampleType are indeed the same type, but Print<Demo::Foo> and Print<Demo::Bar> are different functions and different functions with the same parameter are allowed.
This feels cleaner to me.
You could make it an std::ostream &operator<< instead of a to_string, but that is less flexible.
Also I forgot a bunch of includes, but gcc doesn't care 🤡
 
 
2 hours later…
2:01 PM
Why does this not work plz?
#define PRINT(...)\
{\
    char str[100]={0}; \
    snprintf(str, sizeof(str), "%s", ##__VA_ARGS__); \
    fprintf(stdout, "%s\n", str);\
}

int main(void)
{
    fprintf(stdout, "Hi\n");
    PRINT("hello: %d\n", 55);
    return 0;
}
it is a simplified version of the actual situation
 
you sure you didn't mean
snprintf(str, sizeof(str), ##__VA_ARGS__); \
instead of
snprintf(str, sizeof(str), "%s", ##__VA_ARGS__); \
based on the way you call it
 
Hmmm dunno, let's see...
OK, yhea that seems to work. Weird
int snprintf(char *str, size_t size, const char *format, …);
So I thought it was mandatory to have something like "%s" in there....
and that VA_ARGS was what replaced the "..." from the signature
 
sure you do, but you seem to be making that part of the argument to the macro
 
@PeterT I am sorry. I don't understand what you mean here
Your solution does seem to work indeed. But I dont fully understand why yours does and mine didn't
 
just look at it expaned
snprintf(str, 100, "%s", "hello: %d", 55);
feeding in a format string as an argument to %s doesn't magically trigger another format call
 
2:07 PM
OK, but what does VA_ARGS represent in this expansion in that case?
 
all the arguments you fed in "hello: %d", 55
 
I thought VA_ARGS was just an array of parameters somehow
Oh OK!
it contains the format character stuff as well
 
it contains whatever you passed into PRINT
 
yep yep
Thanks! :)
btw, what does would it change here to add the token pasting stuff here?
ie
snprintf(str, sizeof(str), ##__VA_ARGS__);
instead of
snprintf(str, sizeof(str), __VA_ARGS__);
Both seem to work
Urgh... Actually my situation seems to need some minor modification if VA_ARGS contains the format characters already
I want to prepend it with the current function name. Something along these lines:
snprintf(str, sizeof(str), "%s", __func__,##__VA_ARGS__); \
but that obviously will not work
 
maybe just write a function at that point. Doing weird macro shenanigans is sometimes hard to read as you realized
 
nwp
2:13 PM
No chance to use templates instead of the preprocessor?
 
I think that I really do need a macro in my case, because the PRINT macro is used everywhere in the code. So it needs to be able to print the current function name for debugging purposes
@nwp not in C
 
nwp
@LandonZeKepitelOfGreytBritn Make it 2 calls to snprintf.
 
@nwp hmmm... let s see
Almost...
#include <stdarg.h>
#include <stdio.h>

#define PRINT(...)\
{\
    char str[100]={0}; \
    snprintf(str, sizeof(str), "%s: ", __func__); \
    snprintf(str, sizeof(str),  __VA_ARGS__); \
    snprintf(str, sizeof(str),"%s\n", str); \
    fprintf(stdout, "%s", str);\
}

int main(void)
{
    fprintf(stdout, "Hi\n");
    PRINT("hello: %d %d %s\n", 55, 32, "John");
    fprintf(stdout, "Hi\n");
    return 0;
}
adding the \n leads to str being overwritten somehow
 
snprintf has a return value
use it
 
nothing weird wrt the return value I think
#include <stdarg.h>
#include <stdio.h>

#define PRINT(...)\
{\
    char str[100]={0}; \
    fprintf(stdout, "1: %d\n", snprintf(str, sizeof(str), "%s: ", __func__)); \
    fprintf(stdout, "2: %d\n", snprintf(str, sizeof(str),  __VA_ARGS__)); \
    fprintf(stdout, "3: %d\n", snprintf(str, sizeof(str),"%s\n", str)); \
    fprintf(stdout, "%s", str);\
}

int main(void)
{
    fprintf(stdout, "Hi\n");
    PRINT("hello: %d %d %s\n", 55, 32, "John");
    fprintf(stdout, "Hi\n");
    return 0;
}
returns:
Program returned: 0
Hi
1: 6
2: 18
3: 1

Hi
 
2:22 PM
you are writing to the start of str every time
you can't compare this with stdout, which is a stream
 
Ah yhea you re right I didn't notice it
Stupid me!
this is so obvious
 
@LandonZeKepitelOfGreytBritn why use a macro for this? You're causing the compiler to do twice the work because otherwise you could call vsnprintf directly
 
dont know that one. Let's see the function's documentation...
 
basically the v versions of the function are what does the actual work
snprintf just calls vnsprintf after doing the var-args start stuff
so you can still use a macro.... to call your debug function, but eh....
 
@Mgetz It seems like by using this you'd end up in the same situation. Because I actually want to prepend by the functionname
 
2:30 PM
I noticed that
 
But you still disagree with what I say? :p
 
it was a suggestion naught else
 
nwp
Can you get rid of the temporary buffer? Wherever you end up sending it they probably already have one anyway and if they don't the temporary nature of yours will cause issues.
 
also if you're on linux consider asnprintf and it's v version
 
2:33 PM
@nwp it is a serial port, so I need to convert all integers and what not to string format before sending it. Snprintf does that conversion and requires a buffer somehow
 
nwp
Hmm. Then you need a flush in that macro.
Still, the function name at least can be sent directly.
 
@nwp that part is covered :)
 
@Mgetz dynamic allocation is not a good idea on embedded devices. Especially if everything is time critical
I wouldn t be surprised that snprintf does some dynamic stuff internally though...
 
didn't know if you were doing this on the server or the device. I assumed server
also snprintf may still allocate internally FWIW
 
2:38 PM
Sure, no problem
I also didn t mention it
it would be interesting to be able to scan the entire codebase/binary and so on to know whether some std libs to dynamic allocations under the hood
maybe by running the app through valgrind? This could maybe allow to pinpoint which functions to not use
 
put a breakpoint on the various allocation functions could work
 
unless they are inside glibc or whatever
can't put a breakpoint inside those libraries
snprintf is such an example
btw if I already know which function does dynamic allocation what is the point of putting a breakpoint on it? I meant that it would be interesting to find out which functions inside the entire application do mem allocation under the hood via libs such as glibc or other thirdparty libs
 
what lib are you using on the device?
because on embedded I'd have assumed you'd use MUSL
 
I think glibc or elibc
Nope
It s probably "Newlib" instead
 

« first day (2328 days earlier)      last day (529 days later) »