Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

C++ Template Warnings

$
0
0
I'd like to point out that this isn't my code, and thus my understanding isn't so great. I'm implementing it in my project, however I compile with -Wextra, which produces the following warnings ( and it's bugging the hell out of me ).

In file included from src/bsha1.cpp:3:
src/array.hpp: In instantiation of ‘size_t nil::countof(type (&)[size]) [with type = unsigned int, long unsigned int size = 80ul]’:
src/bsha1.cpp:42:   instantiated from here
src/array.hpp:9: warning: unused parameter ‘array’
src/array.hpp: In instantiation of ‘size_t nil::countof(type (&)[size]) [with type = unsigned int, long unsigned int size = 21ul]’:
src/bsha1.cpp:136:   instantiated from here
src/array.hpp:9: warning: unused parameter ‘array’

src/array.hpp: In instantiation of ‘size_t nil::countof(type (&)[size]) [with type = const std::string, long unsigned int size = 3ul]’:
src/check_revision.cpp:150:   instantiated from here
src/array.hpp:9: warning: unused parameter ‘array’

In file included from src/random.cpp:6:
src/array.hpp: In instantiation of ‘size_t nil::countof(type (&)[size]) [with type = ulong, long unsigned int size = 32ul]’:
src/random.cpp:99:   instantiated from here
src/array.hpp:9: warning: unused parameter ‘array’
src/array.hpp: In instantiation of ‘size_t nil::countof(type (&)[size]) [with type = const bool, long unsigned int size = 11ul]’:
src/random.cpp:109:   instantiated from here
src/array.hpp:9: warning: unused parameter ‘array’


Array.hpp
#ifndef NIL_ARRAY_HPP
#define NIL_ARRAY_HPP

#include <cstddef>

namespace nil
{
	template <typename type, std::size_t size>
		std::size_t countof(type (&array)[size])
	{
		return size;
	}
}
#endif


bsha1.cpp 34-46
void calculate_hash(unsigned * buffer)
{
        unsigned hash_buffer[80];
        unsigned hash, a, b, c, d, e, hash_buffer_offset;

        for(std::size_t i = 0; i < 0x10; i++)
                        hash_buffer[i] = buffer[i + 5];

        for(std::size_t i = 0x10; i < nil::countof(hash_buffer); i++)
        {
                        hash = hash_buffer[i - 0x10] ^ hash_buffer[i - 0x8] ^ hash_buffer[i - 0xE] ^ hash_buffer[i - 0x3];
                        hash_buffer[i] = (1 >> (0x20 - (hash & 0xff))) | (1 << (hash & 0xff));
        }



The cause appears to be when a pointer to an array is passed in, and no size which I believe the template is expecting. I've tried passing size but that just causes errors. I'd expect just passing the pointer in would cause an error, but I guess not.

Oh and believe me I'd love to not use this code, but my only other option is rewriting it, which would take a considerable amount of time.

Viewing all articles
Browse latest Browse all 51036

Trending Articles