#-Wtemplates warning triggered

14 messages · Page 1 of 1 (latest)

lean stirrup
#
#include <iostream>

template <typename T> auto sum(T arg) -> T { return arg; }

template <typename T, typename... Args> auto sum(T start, Args... args) -> T
{
    return start + sum(args...);
}

auto main() -> int
{
    std::cout << sum(1, 2, 3, 4) << '\n';
    return 0;
}

So I've been messing around with templates and variadic arguments and found a warning being triggered...

$make run
mkdir -p build
cmake -S . -B build -G Ninja \
        -DCMAKE_CXX_COMPILER=g++ \
        -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
-- Configuring done (0.0s)
-- Generating done (0.0s)
-- Build files have been written to: /home/amogus/c/l/cpp/build
ln -sf build/compile_commands.json compile_commands.json
ninja -C build
ninja: Entering directory `build'
[1/2] Building CXX object CMakeFiles/app.dir/src/main.cpp.o
/home/amogus/c/l/cpp/src/main.cpp:3:42: warning: template ‘T sum(T)’ declared [-Wtemplates]
    3 | template <typename T> auto sum(T arg) -> T { return arg; }
      |                                          ^
/home/amogus/c/l/cpp/src/main.cpp:4:76: warning: template ‘T sum(T, Args ...)’ declared [-Wtemplates]
    4 | template <typename T, typename... Args> auto sum(T start, Args... args) -> T
      |                                                                            ^
[2/2] Linking CXX executable app
./build/app
10

I know I can always turn off the warnings, but I'm curious as to why the flag thinks lines 3 and 4 are warning-worthy

final forumBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

left anchor
#

doesn't -Wtemplates simply warn on every declaration of a template?

#

if so, it doesn't seem very useful

#

;compile -Wtemplates ```cpp
template<typename> int x;
int main() {}

green crownBOT
#
Compiler Output
<source>:1:24: warning: template 'x' declared [-Wtemplates]
    1 | template<typename> int x;
      |                        ^
left anchor
#

yeah

silent rain
#
 -Wtemplates (C++ and Objective-C++ only)
     Warn when a primary template declaration is encountered.  Some coding rules disallow
     templates, and this may be used to enforce that rule.  The warning is inactive in‐
     side a system header file, such as the STL, so one can still use the STL.  One may
     also instantiate or specialize templates.

:>

lean stirrup
#

well time to remove it then

final forumBOT
# final forum

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

timber silo
#

-Wcpp when

#

warning: C++ code detected [-Wcpp]

left anchor
#

LOL

silent rain