Comments (8)
I don't have a good solution in mind now. Note that it's also really tricky to properly handle the return value:
#include <dbg.h>
template <typename... T>
int sum(T&&... args) {
return (args + ...);
}
template <typename... T>
int calc(T&&... args) {
// Some other logic here in real world...
// This will return 3 instead of 6 :O
// return sum(dbg::identity(args...));
return sum(args...);
}
int main() {
dbg(calc(1, 2, 3));
return 0;
}
A slightly simpler workaround could be wrapping it with tie()
like dbg(std::tie(args...))
.
from dbg-macro.
Hm, also what's interesting that this seems to work!
template <typename... ARGS>
void proxy(ARGS&&... args) {
/* dbg::DebugOutput(__FILE__, __LINE__, __func__)
.print({DBG_MAP(DBG_STRINGIFY, args...)},
{(... + (" " + dbg::type_name<decltype(args)>()))},
args...); */
std::cout << (... + (" " + dbg::type_name<decltype(args)>())) << std::endl;
}
int main(void) {
proxy(1, 'v');
// test('1');
return 0;
}
I think this might fix the issue with finding the appropriate types for the parameter pack.
Though it's still unclear how to threat parameter pack in a different way.
So basically for usual variables we wanna do
#define DBG_TYPE_NAME(x) dbg::type_name<decltype(x)>()
and for parameter pack:
#define DBG_TYPE_NAME(x) (... + (" " + dbg::type_name<decltype(args)>()))
I guess it might be solvable somehow using templates or recursion, but I can't find the solution right now
from dbg-macro.
Thank you for reporting this. It would be great if we could support this.
Can you provide a small example to reproduce the error?
from dbg-macro.
Yes, sure
template <typename... ARGS>
void test(const ARGS&... args) {
dbg("Args: ", args...);
}
int main(void)
{
test(1);
test('1');
return 0;
}
../src/main.cpp:5:21: error: expected ')'
dbg("Args: ", args...);
^
../src/main.cpp:5:3: note: to match this '('
dbg("Args: ", args...);
^
.../dbg.h:808:23: note: expanded from macro 'dbg'
{DBG_MAP(DBG_TYPE_NAME, __VA_ARGS__)}, __VA_ARGS__)
^
../src/main.cpp:5:3: error: expression contains unexpanded parameter pack 'args'
dbg("Args: ", args...);
^ ~~~~
.../dbg.h:806:3: note: expanded from macro 'dbg'
dbg::DebugOutput(__FILE__, __LINE__, __func__) \
The problem seems to occur because the macro expands earlier than the variadic template.
from dbg-macro.
Thank you. I can reproduce it as well.
Unfortunately, I don't really know how to fix it. Any help would be very much appreciated.
from dbg-macro.
Seems the problem in that even the following code doesn't compile:
#include <typeinfo>
template <typename... T>
void test(T&& ...args) {
/* std::cout << typeid(args...).name() << std::endl; */
typeid(args...).name();
// typeid(1).name();
};
int main(void)
{
test('v', 1);
return 0;
}
I'm afraid it cannot be called that way. You can move the type extraction inside the print_impl
, but it will output the wrong info when the variable was not a reference, for example.
from dbg-macro.
Maybe @ShikChen could help out here? (only if you are interested)
from dbg-macro.
I've recently found a hack which might allow this. Consider the following example:
template <int N, typename... Ts>
using NthTypeOf = typename std::tuple_element<N, std::tuple<Ts...>>::type;
template <typename... ARGS>
void f(ARGS&&... args) {
dbg::DebugOutput(__FILE__, __LINE__, __func__)
.print({DBG_MAP(DBG_STRINGIFY, args...)},
{dbg::type_name<NthTypeOf<0, ARGS...>>() + ", " +
dbg::type_name<NthTypeOf<1, ARGS...>>()},
args...); // Here should be logic, which would evaluate args as a
// single variable
}
template <typename... ARGS>
void proxy(ARGS&&... args) {
dbg::DebugOutput(__FILE__, __LINE__, __func__)
.print({DBG_MAP(DBG_STRINGIFY, args...)},
{dbg::type_name<NthTypeOf<0, ARGS...>>() + ", " +
dbg::type_name<NthTypeOf<1, ARGS...>>()},
args...); // Here should be logic, which would evaluate args as a
// single variable
f(args...);
}
int main(void) {
proxy(1, 'v');
// test('1');
return 0;
}
It outputs
[..h_dbg_2/src/main.cpp:20 (proxy)] The number of arguments mismatch, please check unprotected comma
[..h_dbg_2/src/main.cpp:20 (proxy)] args... = 1 (int, char)
[..h_dbg_2/src/main.cpp:20 (proxy)] = 'v' (W5[U-Usrc/main.cpp:20 (proxy)] args... = 1 (int, char)
)
[..h_dbg_2/src/main.cpp:10 (f)] The number of arguments mismatch, please check unprotected comma
[..h_dbg_2/src/main.cpp:10 (f)] args... = 1 (int&, char&)
[..h_dbg_2/src/main.cpp:10 (f)] = 'v' (W5[U-Usrc/main.cpp:10 (f)] args... = 1 (int&, char&)
)
Note that it compiles, and manages to correctly detect when parameter pack variables are lvalue reference. Though it seems to fail when they're rvalue references.
I had to manually expand the macro, to replace the logic for getting the variables' types.
Do you think the logic of dbg can be rewritten to use this?
from dbg-macro.
Related Issues (20)
- Windows Supported? HOT 14
- Broken output of containers HOT 6
- How about output "{?}" string for unknown types instead of failing with static assert? HOT 2
- Allow specifying the output stream HOT 9
- Is support it for android ? HOT 2
- VS2015:error C2912 HOT 5
- debug print on some condition HOT 3
- use in Qt HOT 1
- Add output level HOT 2
- `Type does not support the << ostream operator` on custom type HOT 10
- multithread support HOT 1
- Support always on colors HOT 2
- Nested dbg(...) expansion as not expected HOT 2
- How to force print the full content, not the one with ellipsis HOT 3
- Support for container adapters HOT 1
- Fails with Eigen matrices HOT 7
- Suggest /usr/local/include instead of /usr/include HOT 3
- Report a compiling error with old GCC version (GCC 4.8.5) HOT 1
- Some enhancement proposals for CMake based usecases HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dbg-macro.