Where to find templates, snippets, etc for cpp code?

I’m a blueprint programmer and I am trying to learn more about cpp.

One thing that helped me a lot with learning blueprints is that you have the palette so that you dont need to remember so much.
It also allows you to not need to ever write the same thing twice.

Is there a way to browse cpp templates or script snippets? I am using Rider. An example here:

UE_LOG(LogTemp, Display, TEXT("Message"))

Is there a way I could either find or add a snippet like that to some sort of database so that I never need to type it out manually again?

A fast an easy way is to create an empty .h and define macros there.

e.g.:

// .h
#pragma once

/** Add on screen debug message.
* @param const FString& DebugMessage
*/
#define print_T(DebugMessage) if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::White, TEXT(DebugMessage), false)

/** Add on screen debug message and print to Log.
* @param TChar
* @param ##__VA_ARGS__
*/
#define PrintAndLog(DebugMessage, ...) \
{ \
	FString str = FString::Printf(TEXT(DebugMessage), ##__VA_ARGS__); \
	UE_LOG(LogTemp, Warning,  TEXT("%s"), *str); \
	if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::White, str, false); \
}

Just include in your cpp to use:

print_T("Test");
PrintAndLog("Test %i %i %i", 1, 2, 3);

Would like to know if there is a more convenient way… still learning cpp.

2 Likes

thanks @pezzott1 , that seems like a good idea, I could start building my own little library as I go along.

i saw something about “live templates” but the instructions how to use it is over my head for the time being. I think that may be an official way to save “snippets”. But what I’m also curious about is if there is not some libraries already somewhere? like a plugin you could install that includes lots of commonly used snippets with some descriptions.

Rider: Create a live/surround template | JetBrains Rider

VS: https://docs.wholetomato.com/default.asp?W171

1 Like