c++11 - Moving definition to it's own compilation unit, while keeping declaration in header file? -


i have following, in header file, in 1 of projects;

auto is_base_type   = generic_type_test<const type_expression_base>; auto is_array       = generic_type_test<const type_expression_tarray>; auto is_named_type  = generic_type_test<const type_expression_named>; 

where generic_type_test defined as;

template<typename t> bool generic_type_test(const type_expression& arg) {     return generic_test<type_expression, t>(arg); } 

in same header file.

when compiling bunch of multiple definition linker errors (obviously)

st_pp.o:ast_pp.cpp:(.data+0x0): multiple definition of `ast::is_base_type' st_helper.o:ast_helper.cpp:(.data+0x0): first defined here 

so question is, in it's simplicity, how go moving definitions it's own compilation unit (a ".cpp" file), while keeping declaration in header file?

to jarod42

applying idea, yields;

g++ -o build/ast_helper.o -c --std=c++11 -isrc -ibuild build/ast_helper.cpp build/ast_helper.cpp:11:10: error: conflicting declaration ‘auto ast::is_base_type’     auto is_base_type   = generic_type_test<const type_expression_base>;          ^ in file included build/ast_helper.cpp:1:0: src/ast_helper.hpp:54:10: error: ‘ast::is_base_type’ has previous declaration ‘bool (* ast::is_base_type)(const ast::type_expression&)’     auto is_base_type   = generic_type_test<const type_expression_base>;          ^ 

with lines;

// below line 11 of ast_helper.cpp auto is_base_type   = generic_type_test<const type_expression_base>;  // below line 54 of ast_helper.hpp extern decltype(generic_type_test<const type_expression_base>) is_base_type; 

also know simplest fix, forward function, simplicity of function pointer in way.

i found satisfactory solution, marked these function pointers 'const' alike;

const auto is_base_type   = generic_type_test<const type_expression_base>; const auto is_array       = generic_type_test<const type_expression_tarray>; const auto is_named_type  = generic_type_test<const type_expression_named>; 

as these functions work aliases simply, not prove issue me, ensures compiler doesn't allocate memory pointers, , thereby avoids multiple definition linker errors.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -