3

I learnt from this post here to create helper functions in namespace. 'Helper' functions in C++

//stringhelper.hpp
namespace utility{
static std::string intToString(int integer)
{
    std::stringstream sstream;
    sstream << integer;
    return sstream.str();
}
static void toLowerCase(std::string& y)
{
    std::transform(y.begin(), y.end(), y.begin(), (int(*)(int))tolower);
}
}

I include this header but I got the following warning

'void utility::toLowerCase(std::string&)' defined but not used  

Yes. I used intToString(int integer) but not toLowerCase(std::string&). I don't want to see those warning or divide one helper function per header.

Can anyone suggest a good solution? Should I just disable warning? Thank you

Community
  • 1
  • 1
Mickey
  • 419
  • 7
  • 17
  • What is your compiler? Warnings and their enabling and disabling are compiler-specific. – Alexey Frunze May 21 '12 at 04:36
  • it is GCC, of course. no other compiler would care. – std''OrgnlDave May 21 '12 at 04:45
  • 5
    Given the way you are using them (including from a header file so they have the same definition in each translation unit), it would make more sense to make them `inline` rather than `static`. Is there any particular reason that you wanted to give them internal linkage? – CB Bailey May 21 '12 at 06:39

1 Answers1

3

You have the option to turn off that warning:

-Wno-unused-function

On a function-by-function basis, with GCC, you may define:

void whatever () __attribute__ ((unused));

void whatever () {
    stuff;
}

This tells GCC that the function is meant to possibly be unused.

See compiler docs for more info on attribute

std''OrgnlDave
  • 3,912
  • 1
  • 25
  • 34
  • "OrgnlDave. This method works. However, when I want to combine the definition, compiler returns error of "attribute are not allowed on function definition". For example, this definition return error: static void toLowerCase(std::string& y) __attribute__ ((unused)){ std::transform(y.begin(), y.end(), y.begin(), (int(*)(int))tolower); } – Mickey May 21 '12 at 09:05
  • The implementation of helper functions are written in header. I do not have seperated cpp file. – Mickey May 21 '12 at 09:11
  • @Mickey it is OK, you can declare the signature and then implement it directly underneath if you want, just like what I showed – std''OrgnlDave May 21 '12 at 12:07
  • I have changed my header function same as your demonstration. Just curious. Can this 2 line joined together? – Mickey May 21 '12 at 13:53
  • 1
    @Mickey I don't put a lot of thought into __attribute__ a lot in GCC since I generally write cross-platform code, but I don't think so. function attributes are meant for many things, for instance, forcing things to be inlined or not, so it makes sense for them to be in the declarations and not the implementations. but again I'm not an expert on it. from the documentation I can see no way to do so – std''OrgnlDave May 21 '12 at 14:02