Похожие чаты

Hi. I have two functions: U do_something(T a, T b) {

return U(something); }

template<typename First, typename ... Rest>
U do_something(First first, Rest ... rest)
{
return do_something(first, do_something(rest...));
}

But the compiler can't distinguish what function to call when I call do_something(a, b, c ,d); instead it thinks I'm calling the one with two arguments. What am I doing wrong?

21 ответов

43 просмотра

First of all send your actual code with actual types , U do_something(T a, T b) looks like template syntax, which is very confusing when you put template<typename> on one definition and not on the other. what is T, is it a symbol for you to denote int, bool, types or you forgot to add template<typename T, typename U> there? And share the error code, why won't you share the actual error message?

and this works, https://hastebin.com/efazukuxoq.cpp

Hayk-Zneigras Автор вопроса
Manav | avoid unnecessary messaging me
First of all send your actual code with actual typ...

oh sorry, you are right! I oversimplified the code leaving the actual problem out. I should revise the way I ask questions to avoid this. Here is the actual code including the complete error message: https://pastebin.com/tGt5PwT1 Edit: added a missing function to the pastebin page.

Hayk Zneigras
oh sorry, you are right! I oversimplified the code...

ah, just move callback to the first argument, that'll make it less ambiguous and template deduction will work properly :D https://hastebin.com/okabonowis.cpp

Hayk-Zneigras Автор вопроса
Manav | avoid unnecessary messaging me
ah, just move callback to the first argument, that...

Idk why but I was quite sure it was common practice to place the rest of the arguments after the variadic arguments. I just checked the definition of printf and the variadic arguments come after the rest of the arguments, so yep I'll do so. Thanks! :D

Hayk-Zneigras Автор вопроса
Manav | avoid unnecessary messaging me
ah, just move callback to the first argument, that...

Btw what does TL mean as a name? I know it's just the typename in the template function, but I wonder if it's a common way to name variadic templates, it can be useful.

Hayk Zneigras
Oh got it, thanks again

the way of naming comes from functional programming languages imo, you can easily get the first and the last element using pattern matching in those languages.

Manav | avoid unnecessary messaging me
the way of naming comes from functional programmin...

won't that be, T for type, [] for list, [T] for list of type T, 😜

Manav | avoid unnecessary messaging me
ah, just move callback to the first argument, that...

Tanmay 👆, gists are usually for multiple files and is too verbose for such simple code.

Hayk Zneigras
oh sorry, you are right! I oversimplified the code...

I think this could be compressed into just one function with variadic template, I'll try a few things wait a bit. But still I'd suggest you test extensively the compile times so it's under reasonable parameters. I have seen compile times increasing from a few minutes to few hours because of variadic templates and the binary inflating to very big sizes so be careful.

Hayk-Zneigras Автор вопроса
Manav | avoid unnecessary messaging me
I think this could be compressed into just one fun...

It would make sense to use this function for up to 64 arguments. Is this a significant number? If so I could replace it with a vector and as this is c++11 one will just need to pass the values using an initializer list

Hayk Zneigras
It would make sense to use this function for up to...

64 arguments will create 64 recursions, that's 64 different function implementations. You can see them if you export the symbol table. Ah by initializer_list i meant folding it, i'll give you an example. Btw your previous code didn't have the uint64_t combine_squares(Square square); only the templated version

Hayk-Zneigras Автор вопроса
Manav | avoid unnecessary messaging me
64 arguments will create 64 recursions, that's 64 ...

Right, I remember I had forgotten to add that too to the pastebin, but then updated the link. Probably I missclicked somewhere when editing the message. I'll send that as soon as possibile

Hayk-Zneigras Автор вопроса
Manav | avoid unnecessary messaging me
👍

Ok I'm here. uint64_t combine_squares(Square first, Squade second) { return square_bb(first) | square_bb(second); } uint64_t square_bb(Square square) { return uint64_t(1) << square; }

Hayk Zneigras
Ok I'm here. uint64_t combine_squares(Square firs...

hmm, well you can just use the fold expressions, like this: https://hastebin.com/sawalonoku.cpp

Похожие вопросы

Обсуждают сегодня

Ready for some fun AND a chance to win TKO Tokens? Join us for exciting minigames in our Telegram group! 🕒 Don’t miss out—games start on today 25 October 2024, at 8 PM! Ge...
Milkyway | Tokocrypto
255
Добрый вечер. Есть вопрос, а может и предложение. Был у меня диалог в другой группе о делфи и я задался вопросом: "А нельзя ли в делфи цвет //коментария и {комментария} сде...
Kraszx
24
How are we going bro about the Raids ??
🅿️abby_FX
13
Мдя, прикол, боевая сборка запускается (именно под отладчиком) после F9 примерно полторы минуты (97 секунд если быть точным). Начал копать - проблема детектится сразу - зависа...
Александр (Rouse_) Багель
38
Всем привет! Подскажи, пожалуйста, как передать в TComboBox сразу значение и id записи. На Delphi я делал так: ComboBox1.Items.AddObject('Какое-то значение', Pointer(id запис...
Евгений
10
Keep saying it, it’s Trump or WW3. Iran will be emboldened if Harris wins by cheating. Israel will have to take the initiative against Iran. How has BTC faired everytime the...
Adz | Ferrum Network Product & Service Consultant @ Ferrum Network
1
я так понимаю, я так подозреваю, что создание такого плагина для человека, кто умеет писать плагины для делфи потребует минут 5-10 времени. но это мое подозрение. хотелось бы ...
Kraszx
7
Товарищи, кто работа с iphelper? Или может я в самой логике ошибки фигачу, не пойму.... var ifTable : PMIB_IFTABLE; size, corSize: DWORD; Buffer ...
Warfarellen
4
Здравствуйте, вопрос по структурам данных. Были у вас случаи, когда пришлось писать деревья или двунаправленные списки?
/ /
50
Коллеги, добрый вечер. Создаю коллекцию от TFPGMap, ключ - перечисление, значение - целое. Нужно отсортировать коллекцию по значению. Как это можно сделать?
Kirill Filippenok
11
Карта сайта