template function

Hi, I would like to create a template function that take a function to be called at a specific point. I didn't find the good way to do it.

This is what I got already:

things.hpp
 
template<typename T> void updateScreen(T&&);


things.cpp
1
2
3
4
5
6
7
8
template<typename T>
void updateScreen(T&& f(uint32_t*))
{
    void* pixels;
    // ... stuff
    f(pixels);
    // ... stuff
}


main.cpp
1
2
3
updateScreen([](uint32_t* pixels){
    // ... stuff
});


The compiler give an error on the thing.hpp about something...lambda... is used but never defined.
It's not clear what need to be declared for me.
moechofe wrote:
The compiler give an error on the thing.hpp about something...lambda... is used but never defined.
The implementation (definition) of templates cannot be separated from their declaration, because the full definition is needed for a compiler to generate a function from a function template.

In other words, copy your things.cpp definition into your .hpp file.

PS: Instead of giving your interpretation/summary of the compiler error, it really helps to just verbatim copy-paste what the error is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Example program
#include <cstdint>

using std::uint32_t;

// HPP

template<typename T>
void updateScreen(T f)
{
    void* pixels;
    // ... stuff
    f(static_cast<uint32_t*>(pixels));
    // ... stuff
}

// CPP

int main()
{
    updateScreen([](uint32_t* pixels){
        // ... stuff
    });
}


PPS: Usually, using templates is the preferred way to avoid having to use type-unsafe constructs like void*.
Last edited on
It works, thank you.

By doing that, it forced me to copy the code of updateScreen into the header file, and also force my to move the variable that I'm using in this function.

And now, I got a linker error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// HPP

SDL_Window* window=NULL:
//...
template<typename T>
void updateScreen(T f)
{
    void* pixels;
    // ... stuff
    f(static_cast<uint32_t*>(pixels));
    // ... stuff
}

// CPP

int main()
{
    updateScreen([](uint32_t* pixels){
    });
}


The error is
1
2
/usr/bin/ld: libmylib.a(screen.cxx.o):(.bss+0x0): multiple definition of 'window'; 
libmylib.a(main.cxx.o): (.bss+0x0): first defined here


and I check, I have only one SDL_Window declared, and have #ifndef, #define, #endif stuff on header file.
I also tried with #pragma once

PPS: I'm using SDL2 the SDL_LockTexture() function ask for a void*, that why I got one.
Yeah, I figured it was something external that was forcing you to use void*, I guess that'll have to do.

Multiple definition of 'window'
See this: https://stackoverflow.com/questions/11072244/c-multiple-definitions-of-a-variable

Basically, you need to declare window as extern in your header, and then define it in exactly one .cpp file.

Currently, as you have it, you break the "One-Definition Rule" because you have to remember that all #include does is directly paste the text of the header file; you are essentially declaring the separate globals with the same name in multiple translation units.

e.g.
Change:
1
2
// header.hpp
int* global_var = nullptr;


1
2
3
4
// bar.cpp
#include "header.hpp"

void bar() { *global_var = 42; }


1
2
3
4
// foo.cpp
#include "header.hpp"

int main() { }


to:

1
2
// header.hpp
extern int* global_var;


1
2
3
4
// bar.cpp
#include "header.hpp"

void bar() { *global_var = 42; }


1
2
3
4
5
6
// foo.cpp
#include "header.hpp"

int* global_var = nullptr;

int main() {  }

Last edited on
Topic archived. No new replies allowed.