translation-unit

Should I declare my function template specializations or is defining them enough?

孤者浪人 提交于 2020-05-09 07:34:08
问题 I have some classes which can be checked. The code which implements this declares a function template in a header file and specializes it in different source files: // check.h template <class T> bool check(const T& object); // class1.h struct Class1 {int mass;}; // check_class1.cpp #include "class1.h" #include "check.h" template <> bool check(const Class1& object) {return object.mass < 100;} // class2.h struct Class2 {int price;}; // check_class2.cpp #include "class2.h" #include "check.h"

Template instance in different translation units [duplicate]

痞子三分冷 提交于 2020-01-15 23:37:21
问题 This question already has answers here : Static variable inside template function (7 answers) Closed 4 years ago . As far as I know, each template have different instances on each translation unit, and for my understanding a translation unit is roughly a cpp file. So, if I have a file named test.hpp with the following contents: // test.hpp template <typename T> void test() { static T t = T(0); return t++; } For each translation unit I should have a different instance of test even if the

Template instance in different translation units [duplicate]

浪子不回头ぞ 提交于 2020-01-15 23:36:14
问题 This question already has answers here : Static variable inside template function (7 answers) Closed 4 years ago . As far as I know, each template have different instances on each translation unit, and for my understanding a translation unit is roughly a cpp file. So, if I have a file named test.hpp with the following contents: // test.hpp template <typename T> void test() { static T t = T(0); return t++; } For each translation unit I should have a different instance of test even if the

Is static deprecated when ensuring availability between translation units?

旧时模样 提交于 2020-01-04 01:38:10
问题 From the following stackoverflow answer, the user says: It means that the variable is local to a translation unit (simply put, to a single source file), and cannot be accessed from outside it. This use of static is in fact deprecated in the current C++ Standard - instead you are supposed to use anonymous namespaces: static int x = 0; should be: namespace { int x = 0; } I don't disagree that anonymous namespaces are the preferred method, but is using static really deprecated now? Where does

Why shouldn't C++ operator new/delete/variants be in header files?

主宰稳场 提交于 2019-12-08 15:11:18
问题 Can someone explain the nature of this C++ compile error? I am dabbling in/learning about overloading the global operators new, delete, and their variants. I read a couple of articles on the subject, but I couldn't find one that seems to address this specifically. The Code foo.h : #ifndef foo_h #define foo_h void* operator new(size_t); void* operator new[](size_t); void operator delete(void*); void operator delete[](void*); #endif // foo_h foo.cpp : #include <foo.h> #include <iostream> void*

inline function in different translation units with different compiler flags undefined behaviour?

限于喜欢 提交于 2019-12-06 00:07:35
问题 in visual studio you can set different compiler options for individual cpp files. for example: under "code generation" we can enable basic runtime checks in debug mode. or we can change the floating point model (precise/strict/fast). these are just examples. there are plenty of different flags. an inline function can be defined multiple times in the program, as long as the definitions are identical. we put this function into a header and include it in several translation units. now, what

Using functions that return placeholder types defined in another translation unit

岁酱吖の 提交于 2019-12-01 06:06:57
问题 I'm having some trouble understanding how the C++14 extension of the auto type-specifier described in N3638 can possibly be implemented, and what, exactly, is allowed. Specifically, one of the changes to the standard says, If the declared return type of the function contains a placeholder type, the return type of the function is deduced from return statements in the body of the function, if any. It is easy enough to see how this works when body of the function is in the same file as the