Compiler problem(i think), using templates. forbids comparison between integer and pointer.

closed account (9y8C5Di1)
Main.cpp

cout<<isLowerCased("something")<<endl;

some.h

1
2
3
4
5
6
7
8
9
10
11
12
13
template <typename T>
bool isLowerCased(T elem){
    if(typeid(elem)==typeid(char)){
            if(elem>=97&&elem<=122){return true;}
    }else if(typeid(elem)==typeid(string)){
        for(unsigned int x=0;elem[x]!='\0';x++){
                if(elem[x]>=65&&elem[x]<=90){
                    return false;
                }
        }
    }
return true;
}



Error message:

some.h|4|error: ISO C++ forbids comparison between pointer and integer [-fpermissive]|


I am using codeblocks with the GNU GCC compiler, and it won't let me compile since it thinks i am comparing an integer with a pointer. Does anyone know a way around this error, because the code should be fine, it just won't let me compile and run.
Last edited on
You are trying to compare a pointer with an integer. "something" gives you a char array and when you try to pass it to the function it will decay to a pointer to the first char in the array so the type T will be const char* in this case.

If you make sure that T is std::string,
isLowerCased(std::string("something")) or
isLowerCased<std::string>("something"),
your function will still not work because the whole function has to compile for that type. Line 4 will not compile if elem is of type std::string.

I don't see the benefit of using templates in this case. You can just overload the function for each type that you want to use it for.
1
2
3
4
5
6
7
8
9
10
11
12
13
bool isLowerCased(const std::string& elem) {
	for(unsigned int x=0;elem[x]!='\0';x++){
		if(elem[x]>=65&&elem[x]<=90){
			return false;
		}
	}
	return true;
}

bool isLowerCased(char elem) {
	if(elem>=97&&elem<=122){return true;}
	return true;
}
Last edited on
closed account (9y8C5Di1)
I did previously overload the function, but my computer crashed, lost all my projects, so i am rewriting, and along the way experimenting as i wrote alot of it along time ago in a different way when i was less experienced. And for some reason i dislike overloading when i can with less with complication, but same result create only one function for several purposes.



gives you a char array and when you try to pass it to the function it will decay to a pointer


Ah, i see. I thought it was passes as a string, that would explain the error. And when you mention it, it makes more sense that it is passed as const char*, as type "string" is a class composed of pointer to char, so the core language is used i guess.


your function will still not work because the whole function has to compile for that type.


Another thing i was unaware of about templates.

Your response was just what i needed, i learned important things about templates and passing pure: "osdowa" to a function of template parameter, thank you.

Now when i refer back to overloading for this case, you are absolutely right, even tho i dislike it for strange reasons, maybe it bothers me with extra implementation.

And i finally understand the error message, thank you again.
Templates are useful when you want to deal with a whole *family* of types.

For example, instead of just making your function work on strings only, you could make it work on any type that has a begin() and an end() member functions, and whose element is char (this reinvents a boost function actually).

Then you can add your char and const char* overloads:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <vector>
#include <iostream>
#include <algorithm>
#include <ctype.h>

template <typename T>
bool isLowerCased(const T& elem)
{
    return std::all_of(elem.begin(), elem.end(), ::islower);
}

bool isLowerCased(char c)
{
    return ::islower(c);
}

bool isLowerCased(const char* cstr)
{
    return isLowerCased(std::string(cstr));
}


int main()
{
    std::cout << std::boolalpha
              << isLowerCased(std::string("something")) << '\n'
              << isLowerCased(std::vector<char>{'a','b','c'}) << '\n'
              << isLowerCased("something") << '\n'
              << isLowerCased('a') << '\n';

    std::cout << isLowerCased("sometDFShing") << '\n'
              << isLowerCased('C') << '\n';
}


online demo: http://ideone.com/6mkWwU

Topic archived. No new replies allowed.