[C++] how do you avoid copying declarations into methods' definitions?

Jow Forums, how do you avoid copying declarations into methods' definitions?

1. nested definition (this may cause many problems)
class MyClass {
public:
void myMethod(const int someValue) {
doSomething(someValue);
}
};


2. declaration duplication
class MyClass {
public:
void myMethod(const int someValue);
};

MyClass::myMethod(const int someValue) { // such an annoying duplication!
doSomething(someValue);
}


3. ???

inb4 usage of external preprocessors

Attached: if_source_cpp_3761.png (128x128, 5K)

Other urls found in this thread:

en.cppreference.com/w/cpp/language/inline
twitter.com/NSFWRedditGif

You rewrite it in Rust

You don't avoid copying them. That's part of C++, just deal with it. Maybe modules will change that one day.
Or

MyClass.h:
#ifndef MYCLASS_H
#define MYCLASS_H

class MyClass {
public:
void myMethod(const int someValue);
};

#endif

MyClass.cpp:
#include "MyClass.h"

MyClass::myMethod(const int someValue)
{
doSomething(someValue);
}

I don't care if you find it annoying. This is the correct, standard practice method of doing things.

inline void myMethod(...) wherever you can, when you cannot break dependencies you must duplicate the declaration. inline tells the compiler to ignore duplicate identical definitions, which would appear if you included the same header twice

In general I write everything inline as long as I can, then when the errors start piling up I spend an hour moving everything to external definitions

I think of all the problems with C++, this is the worst (const bullshit is up there too)

if you can afford using a supercomputer to run the executable...

Rust compilation uses LLVM so it's pretty much the same as C or C++ using Clang as to this.
What is your issue with Clang?

1. Nothing bad should ever happen if you include headers twice. See include guards or pragma once
2. inline tells the compiler to avoid making the function *call* (allocating stack, returning, etc), and instead just insert the code in the very place you use your inline function. Although this is only a hint to a compiler.

rust is a bloated layer over c/c++.
I don't care what cc it uses later to compile this bloat.

no code duplication is correct
you need to change it twice, you can totally fuck up your program if you forget to change it in one place (and it may still compile!)

then post the code faggotron

OK sorry, I was wrong, inline makes it so multiple definitions of the function don't cause linker errors. So if you have two cpp files include the same inline function definition, there will be no errors. Without inline, this will cause an error.

And no, inline does not hint the compiler to insert the code in the place you call the inline function. It just prevents multiple definition errors when linking your program.

>it my still compile
no, it won't

see en.cppreference.com/w/cpp/language/inline

Do you think Rust compiles to C?

>inb4 usage of external preprocessors
are there any that address this problem?
or maybe some GCC hacks for this (like do-not-process-methods-definitions-until-whole-class-is-declared switch)?

I want to get rid of "incomplete type 'X' used in nested name specifier" errors without splitting my code into .h and .cpp files.

no, it compiles to c++ then again to c and then to asm

Header file contains only function signatures. If the signatures do not match the implementation, you will get a type error.

This is a useful feature

Traditionally, it compiles to LLVM-IR. There is a Rust to C compiler, however.

unless they do match in a very tricky way
which may be common when using typedefs or even running into as simple problem as int being interpreted as int32_t or int16_t depending on the platform

what is a useful feature?

rewrite it in Java

Attached: 1460934318461.jpg (640x640, 114K)

>or even running into as simple problem as int being interpreted as int32_t or int16_t depending on the platform
If you are using int when you need a specific data size, you're gonna run into problems no matter what.

Java should die!

wait... it was already killed by Oracle.

Use visualsutdio. Right click on the function. Click to move definition.

No. Headers will contain the logical part if your class has class template. Which then will then make using cpp file not possible.

>inline tells the compiler to avoid making the function *call*
Only in your heads. Inline has no real semantics.

Welcome to C++, kid. Enjoy your stay.

Attached: 1535919362364.png (212x200, 17K)

you total faggot
I cannot even express how dumb you are